A Stack Monad in Python

So I saw


where he uses a stack as an example of a state monad.

I get the Maybe monad I think and some others, but the State monad is one more level of abstraction that confuses me still. When I try to reason about it, I fail to see the point sometimes. Pure state to state functions are immediately composable. To also have a return value and return state usually doesn’t seem necessary.

The actual monad is the full state transforming and value returning function. This is very abstract.

Most functions you’ll be writing will return a function that takes state and outputs (val, state) pair. It’s weird.

Though I’d try doing it in python. It is sticky. Maybe I need to churn through it a bit more. The fact that I need to keep putting those toss away functions in there seems strange. Perhaps I need a bit more think. Although the do notation does expand out to that often I think.

It is neato how the associativity kind of works.



I later realized I had forgotten >> aka then


Pretty straightforward. Just added in the throwaway lambda into a function. Can do this manually or by referring back to bind (which is better since then basically just has the logic of bind in it).


Leave a Reply

Your email address will not be published. Required fields are marked *