I need to create stack data type in haskell to be able to write like this:
let a = emptyStack
push 10 a
//[10]
pop a
[]
I want push to look like
push :: a -> Stack a -> Stack a
push a b = a:b
but I have problems with syntax, exactly how to declare this new data type, so that
let a = emptyStack
:t a
would show Stack
any hints on syntax
Let’s look at your implementation for
push. It uses the operator:. You can find out the type of that operator like this:So this operator takes an
a(which represents an arbitrary type), and a sequence ofas, and returns the updated sequence. So your typeStackneeds to be a sequence.If you then define emptyStack like this:
You’ll get the result you’re looking for.
With that help, I think you’ll be able to figure out how to write
pop.