3 ms·
> The state monad is basically a wrapper over a pure function that takes a state and returns a tuple with the result and a new state. So... it's a monad? I don
by darby_nine 2y ago
> The state monad is basically a wrapper over a pure function that takes a state and returns a tuple with the result and a new state.
So... it's a monad? I don't understand why this is worth calling State as distinct from non-State when that doesn't seem to add any meaning. Surely there must be something implied by a State monad that is not implied by a non-State monad. That seems relevant to call out in an introduction.
- trealira 2y agoI 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.
- darby_nine 2y ago> 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.
- trealira 2y agoState 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 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.
- deleted 2y ago[deleted]