[Haskell-cafe] Problem with own written monad

Michael Roth mroth at nessie.de
Mon Jan 7 18:30:57 EST 2008

Jules Bean schrieb:

> data Stack a b = Stack { run :: [a] -> (b, [a]) }

Thank you, that does the trick.

> The correct types for the other functions are:
> push :: a -> Stack a ()
> pop :: Stack a a
> top :: Stack a a
> With those clues I think you will be able to write >>= and return more
> successfully!

Yes, this was the missing link. Because I thought "Stack a a" could be
abbreviated using "Stack a" I run into these problems. This was also the
cause that "push" echoed back the pushed value.

> There are some other interesting combinators to consider, like:
> isolate :: Stack b x -> Stack a x
> -- runs the computation with an empty stack, therefore
> -- guaranteeing it does more pushes than pops

Did you mean:

	isolate :: Stack s1 a -> Stack s2 a
	isolate stack = Stack f where f xs = ( fst $ run stack [], xs)

> and so on.

Yes, I have done: push, pop, top, nop, count, clear, isolate and binop.
All pretty easy, once I understand that "Stack a b" thing.

More information about the Haskell-Cafe mailing list