[Haskell-cafe] laziness blowup exercise

Thomas Hartman tphyahoo at gmail.com
Tue Jul 14 21:02:00 EDT 2009

Challenge: change one function in the following pipeline so that t
doesn't blow up when executed in ghci.

import qualified Data.Set as S
t = last . take (10^6) . iterate f $ S.empty
f s = S.delete 1 . S.insert 1 $ s

Please suggest more of these types of exercises if you have them and
maybe we can collect the folk wisdom into a wiki page and/or exercise
page for beginners.

spoiler for the above problem below :)

myiterate f x =
  let nxt = f x
  in nxt `seq` x : myiterate f nxt

More information about the Haskell-Cafe mailing list