[Haskell-cafe] Stupid newbie question

Brian Hurt bhurt at spnz.org
Fri Jan 5 23:12:57 EST 2007



On Fri, 5 Jan 2007, Jeremy Shaw wrote:

> The easiest solution is to make things a little more strict. For
> example, if you change:
>
> nth i (x:xs) = if i < 0 then Empty else nth (i-1) xs

Even better, if I define:

nth 0 (x:_) = Just x
nth i (_:xs) = if i < 0 then Nothing else nth (i-1) xs
nth i [] = Nothing

makelist i = i `seq` i : (makelist (i+1))

nth 10000000 (makelist 1)

This works like a charm.  Thanks.

Brian



More information about the Haskell-Cafe mailing list