[Haskell-beginners] Problem implementing a stack

Jacob Faren shujinw at gmail.com
Tue Dec 8 20:17:25 EST 2009

Hello. I'm implement data structures (a stack, here) to get familiar with
Haskell. It's not going very well. I have the following code...

    -- test.hs
    data Stack a = Top (Stack a) | Layers [a] deriving (Show)

    stack :: Stack a
    stack = Top (Layers [])

    push :: Stack a -> a -> Stack a
    push (Top (Layers ls)) value = Top (Layers (value:ls))

And I run the following in ghci...

    Prelude> :l test
    [1 of 1] Compiling Main             ( test.hs, interpreted )
    Ok, modules loaded: Main.
    *Main> push stack 4
    Top (Layers [4])
    *Main> let st = stack
    *Main> push st 4

The output of the last command never resolves. I have to kill it. Might
anyone have an idea why the push function is failing with st, but not
with stack?

I know there are other stack implementations that work, but I really
want to know what's going on here.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20091208/dbf45886/attachment.html

More information about the Beginners mailing list