[Haskell-cafe] Ultra-newbie Question

Jean-Marie Gaillourdet jmg at gaillourdet.net
Mon Sep 20 09:47:49 EDT 2010

Hi James,

On 20.09.2010, at 15:20, James Andrew Cook wrote:

>> Lazyness helps helps to reduce work if your input list is lazily constructed and your function forces the returned element. Then you don't have to force all elements of the list, only the last one. Let's say l = [e_0, ..., e_n]. All the e_i are expensive calculations.
>>> g :: [a] -> a
>>> g xs = x `seq` x
>>> where
>>>   x = head (reverse xs)
> Can "x `seq` x" have any different strictness than just plain x?  I may be wrong, but I don't think so.  Essentially, it's saying that "when x is needed, evaluate x to WHNF and then return x".

Yes, I think you are right. I was trying to force evaluation of the returned element. Something like that:

> {-# LANGUAGE BangPatterns #-}
> g :: [a] -> a
> g xs = x
>   where
>     !x = head (reverse xs)

-- Jean

More information about the Haskell-Cafe mailing list