[Haskell-beginners] Recursive Let
Brandon S. Allbery KF8NH
allbery at ece.cmu.edu
Mon Feb 9 23:42:23 EST 2009
On 2009 Feb 9, at 20:43, Tom Poliquin wrote:
> I'm working on learning arrows.
> This has led me to ArrowLoop, which then led me
> to trying to understand the following,
>> tracePlus b = let (c,d) = (d,b:d)
>> in c
>> main = do
>> w <- return $ take 10 $ tracePlus 7
>> print w
> 1) How does recursion happen?
> Being an ex-Schemer I recalled that let x = 3
> get translated into something like
> (lambda (x) ... ) 3
> So now there's a function involved. It's clear that b:d is
> (cons b d)
> another function. So with appropriate plumbing
> I could see the thing recurses but the Haskell viewpoint
> eludes me.
The trick is that the c and d on both sides of the equal sign are
identical. Since this asserts that d = b:d, Haskell repeatedly
prepends b to d (lazily, so "take 10" halts the recursion).
let (c,d) = (d,b:d) in c = d
let (c,d) = (b:d,b:b:d) in c = b:d
let (c,d) = (b:b:d,b:b:b:d) in c = b:b:d
As long as something consumes elements from c, that let will continue
to expand recursively.
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university KF8NH
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 195 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/beginners/attachments/20090209/7e0ecc3d/PGP.bin
More information about the Beginners