[Haskell-beginners] Seq question

Tim Perry tim.v2.0 at gmail.com
Tue Apr 17 23:41:15 CEST 2012


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 <blackcat at pro-ns.net> wrote:

> 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 at haskell.org
> http://www.haskell.org/**mailman/listinfo/beginners<http://www.haskell.org/mailman/listinfo/beginners>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120417/23b548af/attachment.htm>


More information about the Beginners mailing list