[Haskell-beginners] Bug or Intended behavior? ( Exception: stack overflow )

Seung Soo, Ha sungsuha at gmail.com
Mon Jan 24 11:59:15 CET 2011


my setup: phenom II quadcore, 4GB mem Ubuntu Maverick 

While implementing my own reverse in  "99 haskell questions",

I made the following mistake:
(Caution: stack over flow causing code below)


reverse'::[a] -> [a]
reverse' [] = []
reverse' [x] = [x]
reverse' xs = last xs : init (reverse' xs)

The last line is the wrong part. It should be 
reverse' xs = last xs : reverse' (init xs)

anyway, the original code seems to cause an infinite recursion
 by repeating the last part forever. When I try this out on ghci:


*Main> reverse' [1,2,3,4,5]
[5*** Exception: stack overflow
*Main> 

The problem is, unless I manually exit ghci, the "stack" does not seem to be
 freed (garbage collected?) and memory remains fully occupied by ghci.

I tried waiting a bit, but it stayed the same. 

Is this expected behavior? Shouldn't the GC kick in? 
Is this orthogonal of the GC?
I think this would help me understand haskell and GHC much better.




More information about the Beginners mailing list