Hint: here is how you might implement your stack operations using the built-in list:
push :: a -> [a] -> ((),[a]) -- return a tuple containing a 'nothing' and a new stack
push elem stack = ((), (:) elem stack)
pop :: [a] -> (a, [a]) -- return a tuple containing the popped element and the new stack
pop [] = error "Can't pop from an empty stack!"
pop ((:) x stack) = (x, stack)
(:) x xs
is an alternative way of writing x:xs
.
To do this on your own MyStack
type, note that your Empty
actually works just like []
, and Elem
is equivalent to (:)
. I won't give you the code to do this outright, because figuring it out for yourself is half the fun!