[Haskell-cafe] Stack ADT?

Rahul Kapoor rk at trie.org
Fri Feb 5 11:02:08 EST 2010

> What's going on here? I'm not even calling function POP.
> *Data.Stack> let s2 = pop s1
> *Data.Stack> top s2
> *** Exception: Stack.hs:8:0-28: Non-exhaustive patterns in function pop

Haskell being a non strict language, does not evaluate pop s1 when you define
let s2 = pop s1.
but when you try to use s2 by evaluating "top s2" "pop s1" needs to be evaluated
leading to the error in "pop", since the definition of pop, does not
deal with the case when
the Stack is empty (Stack []).

> pop (Stack (_:xs)) = Stack xs
> top (Stack (x:_)) = x

More information about the Haskell-Cafe mailing list