Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use a vector to cache results within a Haskell function?

I have a computationally expensive vector I want to index into inside a function, but since the table is never used anywhere else, I don’t want to pass the vector around, but access the precomputed values like a memoized function.

The idea is:

cachedFunction :: Int -> Int
cachedFunction ix = table ! ix
    where table = <vector creation>

One aspect I’ve noticed is that all memoization examples I’ve seen deal with recursion, where even if a table is used to memoize, values in the table depend on other values in the table. This is not in my case, where computed values are found using a trial-and-error approach but each element is independent from another.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How do I achieve the cached table in the function?

>Solution :

According to this answer, GHC will never recompute values declared at the top-level of a module. So by moving your table up to the top-level of your module, it will be evaluated lazily (once) the first time it’s ever needed, and then it will never be requested again. We can see the behavior directly with Debug.Trace (example uses a simple integer rather than a vector, for simplicity)


import Debug.Trace

cachedFunction :: Int -> Int
cachedFunction ix = table + ix

table = traceShow "Computed" 0

main :: IO ()
main = do
  print 0
  print $ cachedFunction 1
  print $ cachedFunction 2

Outputs:

0
"Computed"
1
2

We see that table is not computed until cachedFunction is called, and it’s only computed once, even though we call cachedFunction twice.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading