[Haskell-cafe] Stupid newbie question

Brian Hurt bhurt at spnz.org
Fri Jan 5 20:17:33 EST 2007

My apologies for wasting bandwidth on what I'm sure is a stupid newbie 


-- Reimplementing the wheel here, I know

data Option a = Some a | Empty
 	deriving (Eq,Ord,Show,Read)

nth 0 (x:xs) = Some x
nth i (x:xs) = if i < 0 then Empty else nth (i-1) xs
nth i [] = Empty


nth 1000000 [1..10000000]

returns the right answer, while:

nth 1000000 [1..]

blows stack.  Furthermore, given:

makelist i = i : (makelist (i-1))

Why is it that:
nth 1000000 (makelist 1)

blows stack?  What are the rules for being tail recursive in Haskell?  I'm 
an Ocaml programmer, so I'm familiar with this problem, I just don't see 
the solution.  Some help would be appreciated.

Thanks in advance,

More information about the Haskell-Cafe mailing list