[Haskell-cafe] forever function laziness

Albert Y. C. Lai trebla at vex.net
Thu Dec 26 19:09:54 UTC 2013


On 13-12-23 09:02 PM, Eduardo Sato wrote:
> The recursive definition does make sense to me in a mathematical way, but I can't figure out how it works under the hood in terms of thunks.

> To tell you the truth, I don't know how laziness works in general in haskell.

For lazy evaluation, see my
http://www.vex.net/~trebla/haskell/lazy.xhtml

The following produces and destroys 10 cons cells. Unless the compiler 
does smart things.

     main = print (take 10 (plenty 5))
     plenty n = n : plenty n

The following produces and reuses 1 cons cell.

     main = print (take 10 (plenty 5))
     plenty n = s where s = n : s
     -- or, let s = n : s in s

Reusing comes from sharing. Sharing comes from aliasing. Aliasing is 
using the same name s. Self-aliasing is then using the same name s on 
both sides of =.

It is best to draw some diagrams. I am too lazy to do it here. But I did 
some in my lazy evaluation article, and it shows you how to do more on 
your own.

forever is similar.

> The "tying the knot" article on the wiki is pretty mind bending too.

Most authors on the haskell wiki are driven by excitement. The problem 
with excitement is that excited authors lose readers by telling too much 
and starting too high.


More information about the Haskell-Cafe mailing list