Stack-0.1.0: src/Data/Stack.hs
-- | Abstract Stack data type
module Data.Stack (
Stack,
stackNew,
stackIsEmpty,
stackPush,
stackPeek,
stackPop,
stackSize,
)
where
-- | Abstract Stack data type
data Stack a = Stack [a] deriving (Read,Show)
-- | Create new Stack
stackNew :: Stack a
stackNew = Stack []
-- | Test if stack is empty
stackIsEmpty :: Stack a -> Bool
stackIsEmpty (Stack []) = True
stackIsEmpty (Stack _) = False
-- | Push item onto Stack
stackPush :: Stack a -> a -> Stack a
stackPush (Stack items) item = Stack (item : items)
-- | Pop most recently added item without removing from the Stack
stackPeek :: Stack a -> a
stackPeek (Stack items) = head items
-- | Pop most recently item from Stack
stackPop :: Stack a -> (Stack a, a)
stackPop (Stack items) = (Stack (tail items), stackPeek (Stack items))
-- | Compute number of elements contained in the Stack
stackSize :: Stack a -> Int
stackSize (Stack items) = length items