[Haskell-cafe] Strict evaluation not working?
Keith Wansbrough
Keith.Wansbrough at cl.cam.ac.uk
Tue Oct 12 12:20:26 EDT 2004
> main =
> let x = zipWith (+) [5..] [6..]
> in putStrLn $ show $ x `seq` head x
>
> I expected this program not to terminate - because of the seq-Operator,
> but it happily returns 11 in ghc as well as in ghci. What do I make
> wrong?
"seq" forces its argument to "Weak Head Normal Form". In plain
English, that means that it insists that the topmost constructor is
specified (here whether x is [] or (:)), but nothing more.
If you want to force the entire data structure, you have to walk over
it, forcing every part of it. Often people use a DeepSeq class to
achieve this.
But if you are just learning Haskell, you almost certainly don't need
to do this. Just make use of the laziness, and learn to love it!
Come back to "seq" when you are writing a large application, and
profiling tells you you have a space leak.
--KW 8-)
More information about the Haskell-Cafe
mailing list