[Haskell-beginners] Caching evaluation of lazy lists

Philip Scott haskell-beginners at foo.me.uk
Fri Oct 23 07:51:52 EDT 2009

Hi folks,

Quick question for you. Suppose I have some lazy list, x, which is very 
expensive to evaluate, and I want to do something with it more than 
once; for example:

f x = g (take 5 x) (take 6 x)

Is Haskell clever enough to only evaluate the list as much as is needed, 
or will it evaluate it once to get the first five values, and again to 
get the first 6 (when really it only needed to get one more). Or is it 
really really clever, and works out it needs to take 6 to begin with, 
then just give you the first 5 for the first call?

More tricky - is there a way to make that cache (if it exsists) persist 
in an interactive session? Eventually I am intending to write my own 
application with a little console at the bottom which does some 
ghci-esque magic, but for now lets say I am in ghci and I want to call f 
again with the same list. Is there a way to avoid it from forcing a 
recomputation of my lazy list?

Any advice greatly appreciated,


More information about the Beginners mailing list