Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Lazy evaluation and list comprehensions (Obscaenvs)
2. Re: Lazy evaluation and list comprehensions
(Felipe Almeida Lessa)
----------------------------------------------------------------------
Message: 1
Date: Wed, 08 Aug 2012 22:48:16 +0200
From: Obscaenvs <[email protected]>
Subject: [Haskell-beginners] Lazy evaluation and list comprehensions
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Hi fellow Haskellers! Trying literary style here, lets just hope it
plays well with your email clients.
Suppose we define a list - somewhat arbitrarily - as
> xs = [1..42] :: [Int]
Next, we let x and ys be
> x = 17 -- also somewhat arbitrarily
> ys = [ y | y <- xs, y >= x ]
My question: in which WHNF state is ys? y and x are evaluated in
applying >= to them - at least in this particular instance of Ord
(Int). Are these evaluated values then copied to the new list ys, or
are they just stored as thunks? I would guess that they are just
thunked, and that the state of ys is [*thunk*,*thunk*,...,*thunk*]
since it cannot be known beforehand to what extent the predicate
reduces its arguments (in this case >= ), but I don't know for sure.
I thought I had sufficient knowledge about thunks, WHNF, NF and lazy
evaluation, but this one does me in. I guess it is because I haven't
trawled through Okasaki's book yet :)
------------------------------
Message: 2
Date: Wed, 8 Aug 2012 18:14:18 -0300
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] Lazy evaluation and list
comprehensions
To: Obscaenvs <[email protected]>
Cc: [email protected]
Message-ID:
<CANd=ogghzl+r18+r61zsflzuhp1xtk4sxcypndquybfxpev...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
Just use GHCi's :print command =).
Prelude> let xs = [1..42] :: [Int]; x = 17; ys = [ y | y <- xs, y >= x ]
Prelude> :print xs
xs = (_t1::[Int])
Prelude> :print ys
ys = (_t2::[Int])
Initially nothing is evaluated (as expected).
Let's try forcing ys to WHNF:
Prelude> ys `seq` () -- forcing to WHNF
()
Prelude> :print xs
xs = 1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : 9 : 10 : 11 : 12 : 13 : 14 :
15 : 16 : 17 : (_t3::[Int])
Prelude> :print ys
ys = 17 : (_t4::[Int])
So xs had to be forced until the first element of ys appeared (since
WHNF decides between [] and _:_). We didn't explicitly evaluate ys's
first element but as it needed to be evaluated before it already
appears evaluated now.
Same thing if you continue forcing ys:
Prelude> tail ys `seq` () -- forcing tail to WHNF
()
Prelude> :print xs
xs = 1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : 9 : 10 : 11 : 12 : 13 : 14 :
15 : 16 : 17 : 18 : (_t5::[Int])
Prelude> :print ys
ys = 17 : 18 : (_t6::[Int])
Cheers,
--
Felipe.
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 50, Issue 9
****************************************