Lazy evaluation

The history of Haskell is deeply entwined with the history of lazy evaluation.


"Laziness was undoubtedly the single theme that united the various groups that contributed to Haskell's design...

Once we were committed to a lazy language, a pure one was inescapable."

 --History of Haskell, Hudak et al

Thanks to lazy evaluation, we can still consume the undoomed part of this list:

doomedList = [2, 3, 5, 7, undefined]
take 0 xs = []
take n (x:xs) = x : (take (n-1) xs)

main = do print (take 4 doomedList)

The take function is lazy because the cons operator (:) is lazy (because all functions in Haskell are lazy by default).

A lazy cons evaluates only its first argument, while the second argument, the tail, is only evaluated when it is selected. ...

Get Haskell Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.