[Haskell-cafe] Re: The State Monad

Paul Hudak paul.hudak at yale.edu
Fri Oct 8 16:00:47 EDT 2004


Sorry to nit-pick, but state monads are NOT syntactic sugar -- they're 
just an example of good old data/functional abstraction, that also 
happens to be in the form of a monad.

On the other hand, Haskell's "do" notation -- now THAT'S syntactic sugar :-)

   -Paul


Ben Lippmeier wrote:
> 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