Stacks
newtype Stack a = Stk [a] deriving Show push x (Stk xs) = Stk (x:xs) pop (Stk []) = error "pop from an empty stack" pop (Stk (_:xs)) = Stk xs top (Stk []) = error "top from an empty stack" top (Stk (x:_)) = x emptyStack = Stk [] stackEmpty (Stk []) = True stackEmpty (Stk _ ) = False
ตัวอย่างการเรียกใช้งาน
$ hugs Stacks.hs Main> push 5 (push 3 (push 1 emptyStack)) Stk [5,3,1] Main> top (push 5 (push 3 (push 1 emptyStack))) 5 Main> pop (push 5 (push 3 (push 1 emptyStack))) Stk [3,1]


