Hint: here's how to implement 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 to write x:xs .
To do this in the MyStack native type, note that your Empty works the same as [] , and Elem equivalent (:) . I will not give you the code to do it directly, because figuring it out for myself is half the fun!
source share