[Haskell-cafe] laziness blowup exercise

Ryan Ingram ryani.spam at gmail.com
Wed Jul 15 12:35:11 EDT 2009


On Tue, Jul 14, 2009 at 6:02 PM, Thomas Hartman<tphyahoo at gmail.com> wrote:
> myiterate f x =
>  let nxt = f x
>  in nxt `seq` x : myiterate f nxt

iterate' f x = x `seq` x : iterate' f (f x)
seems better; it doesn't evaluate list elements you don't visit.

> let test = 1 : 2 : 3 : undefined in last $ take 4 $ myiterate (test !!) 0
*** Exception: Prelude.undefined

> let test = 1 : 2 : 3 : undefined in last $ take 4 $ iterate' (test!!) 0
3

  -- ryan


More information about the Haskell-Cafe mailing list