-- Implements a stack data structure -- by Scot Drysdale on 8/25/07 module Stack (Stack(Stack), makeStack, push, pop, isEmpty) where data Stack a = Stack [a] deriving Show -- Creates an empty stack makeStack :: Stack a makeStack = Stack [] -- Adds a new value to the top of a stack, returning the updated stack push :: a -> Stack a -> Stack a push x (Stack s) = Stack (x:s) -- Removes the top value in a stack, returning both the value and -- the updated stack as a pair. pop :: Stack a -> (a, Stack a) pop (Stack (x:s)) = (x, Stack s) pop _ = error "Cannot pop an empty stack" -- returns true if stack is empty isEmpty :: Stack a -> Bool isEmpty (Stack []) = True isEmpty _ = False