[Haskell-beginners] Seq question
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:
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.
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..]
> Beginners mailing list
> Beginners at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners