Re:State Monad for the Rest of Us
by trealira on Friday August 09, 2024 @11:22AM
(#41202733)
I don't see why you think all monads are just like the State monad. The Maybe and Either monads have nothing to do with state, for example.
[ Parent
| on HN ]
Re:State Monad for the Rest of Us
by darby_nine on Friday August 09, 2024 @12:25PM
(#41203282)
> The Maybe and Either monads have nothing to do with state, for example.
See this is what I'm talking about. What does `state` mean to you where monads don't have any relation? It's just an abstract structure—what makes something state or not is the context.
State can be modeled as a monad, but monads aren't a synonym for state. Just like squares are technically rectangles, but "rectangle" isn't a synonym for "square."
How do I think of the State monad?
- Like a Mealy machine: https://en.wikipedia.org/wiki/Mealy_machine
- Like a pure way to compose functions that use a nameless, global mutable variable.
- As a newtype wrapper over the type "s -> (a, s)" where s is the type of the state and a is the type of the computation.