-- File: src/functional/haskell/Stack.hs
-- Copyright (C) 2010 Gregory D. Weber
-- Stacks in Haskell

module Stack where

data Stack a = Stack [a]
    deriving (Show)

newStack :: Stack a
newStack = Stack []

isEmptyS :: Stack a -> Bool
isEmptyS (Stack xs) = null xs

push :: a -> Stack a -> Stack a
push x (Stack xs) = Stack (x:xs)

peek :: Stack a -> a
peek (Stack []) = error "peek: empty stack"
peek (Stack (x:xs)) = x

pop :: Stack a -> Stack a
pop (Stack []) = error "pop: empty stack"
pop (Stack (x:xs)) = Stack xs

-- combine peek and pop, returning a pair (top, stack')
pop' :: Stack a -> (a, Stack a)
pop' stk = (peek stk, pop stk)

