
I've got the following code, and I'm not sure why the seq seems to be doing nothing. The code should never terminate if the seq were forcing the full evaluation of y. Instead, it runs just fine (though stack overflows if I ask for too many elements of primes). I didn't see anything in the tutorial section of haskell.org that seems to explain this -- pointers to a useful source would be welcome. sieve :: [Int] -> [Int] sieve xs = let ys = filter (\z -> 0 /= z `mod` head xs) xs in seq ys $ head xs:sieve ys primes = sieve [2..] thanks Lee

seq evaluates to Weak Head Normal Form (WHNF). WHNF is the first
contructor. So your use of seq only evaluates the first number and the
cons. I.E., it evaluates to:
s:(Thunk)
You are expecting seq to work like deepseq and evaluate the entire list.
Also, mod is slower than rem and the meaning doesn't matter in a sieve so
change mod to rem for faster code.
Tim
On Tue, Apr 17, 2012 at 1:56 PM, Lee Short
I've got the following code, and I'm not sure why the seq seems to be doing nothing. The code should never terminate if the seq were forcing the full evaluation of y. Instead, it runs just fine (though stack overflows if I ask for too many elements of primes). I didn't see anything in the tutorial section of haskell.org that seems to explain this -- pointers to a useful source would be welcome.
sieve :: [Int] -> [Int] sieve xs = let ys = filter (\z -> 0 /= z `mod` head xs) xs in seq ys $ head xs:sieve ys
primes = sieve [2..]
thanks Lee
______________________________**_________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/**mailman/listinfo/beginnershttp://www.haskell.org/mailman/listinfo/beginners

On Tue, Apr 17, 2012 at 02:41:15PM -0700, Tim Perry wrote:
seq evaluates to Weak Head Normal Form (WHNF). WHNF is the first contructor. So your use of seq only evaluates the first number and the cons. I.E., it evaluates to: s:(Thunk)
Actually, it doesn't even force the first number. You just get Thunk : Thunk -Brent

On Wed, 18 Apr 2012 16:45:05 +0200, Brent Yorgey
On Tue, Apr 17, 2012 at 02:41:15PM -0700, Tim Perry wrote:
seq evaluates to Weak Head Normal Form (WHNF). WHNF is the first contructor. So your use of seq only evaluates the first number and the cons. I.E., it evaluates to: s:(Thunk)
Actually, it doesn't even force the first number. You just get
Thunk : Thunk
A nice way to demonstrate this, is the following GHCi session: Prelude> undefined `seq` print "OK" *** Exception: Prelude.undefined Prelude> [undefined] `seq` print "OK" "OK" Regards, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html Haskell programming --

Henk-Jan van Tuly is correct. Oops! Sorry I was mis-informed. By the way,
there is an excellent post about this topic on StackOverflow.com. It
covers, seq, weak-head-normal-form, normal form, and thunks. See here:
http://stackoverflow.com/questions/6872898/haskell-what-is-weak-head-normal-...
Hope that makes up for my earlier comment.
Tim
On Thu, Apr 19, 2012 at 2:31 AM, Henk-Jan van Tuyl
On Wed, 18 Apr 2012 16:45:05 +0200, Brent Yorgey
wrote: On Tue, Apr 17, 2012 at 02:41:15PM -0700, Tim Perry wrote:
seq evaluates to Weak Head Normal Form (WHNF). WHNF is the first contructor. So your use of seq only evaluates the first number and the cons. I.E., it evaluates to: s:(Thunk)
Actually, it doesn't even force the first number. You just get
Thunk : Thunk
A nice way to demonstrate this, is the following GHCi session: Prelude> undefined `seq` print "OK" *** Exception: Prelude.undefined Prelude> [undefined] `seq` print "OK" "OK"
Regards, Henk-Jan van Tuyl
-- http://Van.Tuyl.eu/ http://members.chello.nl/**hjgtuyl/tourdemonad.htmlhttp://members.chello.nl/hjgtuyl/tourdemonad.html Haskell programming --
______________________________**_________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/**mailman/listinfo/beginnershttp://www.haskell.org/mailman/listinfo/beginners

On Thursday 19 April 2012, 23:13:48, Tim Perry wrote:
Henk-Jan van Tuly is correct. Oops! Sorry I was mis-informed.
Note, however, that in the case here, the data-dependencies force the evaluation of the first number in order to determine the outermost constructor. So actually, your
So your use of seq only evaluates the first number and the cons. I.E., it evaluates to: s:(Thunk)
was factually correct (in this special case).
By the way, there is an excellent post about this topic on StackOverflow.com. It covers, seq, weak-head-normal-form, normal form, and thunks. See here: http://stackoverflow.com/questions/6872898/haskell-what-is-weak-head-no rmal-form/6889335#6889335
Hope that makes up for my earlier comment.
Tim
On Thu, Apr 19, 2012 at 2:31 AM, Henk-Jan van Tuyl
wrote: On Wed, 18 Apr 2012 16:45:05 +0200, Brent Yorgey
wrote: On Tue, Apr 17, 2012 at 02:41:15PM -0700, Tim Perry wrote:
seq evaluates to Weak Head Normal Form (WHNF). WHNF is the first contructor. So your use of seq only evaluates the first number and the cons. I.E., it evaluates to: s:(Thunk)
Actually, it doesn't even force the first number. You just get
Thunk : Thunk
A nice way to demonstrate this, is the following GHCi session: Prelude> undefined `seq` print "OK" *** Exception: Prelude.undefined Prelude> [undefined] `seq` print "OK" "OK"
Regards, Henk-Jan van Tuyl
participants (5)
-
Brent Yorgey
-
Daniel Fischer
-
Henk-Jan van Tuyl
-
Lee Short
-
Tim Perry