[Haskell-cafe] Re: The State Monad
Ben Lippmeier
Ben.Lippmeier at anu.edu.au
Fri Oct 8 00:22:26 EDT 2004
John Goerzen wrote:
> Which leaves me with an odd curiosity -- I still can't figure out how
> state monads are anything but syntactic sugar (and lead more to
> spaghetti code at that <g>)
Perhaps because state monads = syntactic sugar.
The state monad is just a nice(er) way of passing around some global
state (Junk).
Without state monads
f :: Junk -> a -> (Junk, b)
With state monads,
f :: a -> State Junk b
....
Though if some function doesn't need to 'modify' your Junk, you find
yourself having to re-factor things like,
decend :: Junk -> Exp -> Exp
decend state (Node a t1 t2)
= Node a (decend state t1) (decend state t2)
into
decend :: Exp -> State Junk Exp
decend (Node a t1 t2)
= do
t1' <- decend t1
t2' <- decend t2
return $ Node a t1' t2'
.. which IMHO is not as pretty.
Ben.
More information about the Haskell-Cafe
mailing list