[Haskell-beginners] Question about lazy evaluation

Brandon Allbery allbery.b at gmail.com
Fri Sep 9 00:09:33 CEST 2011


On Thu, Sep 8, 2011 at 01:56, Zhi-Qiang Lei <zhiqiang.lei at gmail.com> wrote:

> When ('f' : "oo") ++ "bar" becomes 'f' : ("oo" ++ "bar") and then becomes
> 'f' : ('o' : ("o" ++ "bar")), we still need 'f', don't we?


Haskell lists are singly-linked lists, not arrays or double-linked lists or
etc.  Once we've evaluated past a given ":", there is no way to go back to
what precedes it; it's no longer reachable (unless the caller, who has been
passed the earlier part, is holding onto it) and the garbage collector will
reclaim it.

Put slightly differently:  as you've phrased it, evaluation would expand
each element but always start stepping into it from the very start.  That's
not what happens; as the evaluator steps through the list, it throws away
its reference to the earlier part completely.  So it never becomes 'f' :
('o' : ("o" ++ "bar"))) because when the evaluator is at the 'o' it has
completely forgotten about the 'f'.  It's got 'o' : ("o" ++ "bar") and any
remaining reference (if there is one) to the 'f' is held by something else.

-- 
brandon s allbery                                      allbery.b at gmail.com
wandering unix systems administrator (available)     (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110908/154bea20/attachment.htm>


More information about the Beginners mailing list