[Haskell-cafe] Laziness question
wren ng thornton
wren at freegeek.org
Sat Jul 31 16:58:05 EDT 2010
Brandon S Allbery KF8NH wrote:
> michael rice wrote:
>> Are you saying:
>>
>> [ head x ] -> [ *thunk* ] and length [ *thunk* ] -> 1, independent of
>> what *thunk* is, even head [], i.e., *thunk* never needs be evaluated?
>
> Exactly. (I was being cagey because the first response was cagey, possibly
> suspecting a homework question although it seems like an odd time for it.)
>
> length not only does not look inside of the thunk, it *can't* look inside
> it; all it knows is that it has a list, it specifically does *not* know what
> that list can hold. So the only thing it can do is count the number of
> "unknown somethings" in the list.
Not entirely true:
stupidlyStrictLength :: [a] -> Integer
stupidlyStrictLength [] = 0
stupidlyStrictLength (x:xs) = x `seq` 1 + stupidlyStrictLength xs
Though, of course, if we actually wanted this function we should use an
accumulator in order to avoid stack overflow when evaluating the
(1+(1+...0)) thunk at the end.
--
Live well,
~wren
More information about the Haskell-Cafe
mailing list