[Haskell-cafe] Re: about Haskell code written to be "too smart"

Jonathan Cast jonathanccast at fastmail.fm
Wed Mar 25 12:10:46 EDT 2009


On Wed, 2009-03-25 at 15:32 +0000, Simon Marlow wrote:
> Jonathan Cast wrote:
> > On Wed, 2009-03-25 at 15:09 +0000, Simon Marlow wrote:
> >> the ordering that the state monad expects 
> >> (and I can never remember which way around they are in Control.Monad.State).
> > 
> > Really?  I found it obvious once I figured out it how simple it made
> > (>>=).  With the order from Control.Monad.State (with constructors
> > ignored):
> > 
> >     a >>= f = \ s -> case s a of
> >        (x, s') -> f x s'
> > 
> > Reversing the order of the components of the result gives you
> > 
> >     a >>= f = \ s -> case s a of
> >         (s', x) -> f x s'
> > 
> > which just looks weird.
> 
> It might look weird to you, but that's the way that GHC's IO and ST monads 
> do it.  It looks perfectly natural to me!

Right.  Consider this an argument for fixing IO/ST(/STM?) to conform to
the self-evidently correct ordering of Control.Monad.State :)

> (and you have the a and s the 
> wrong way around in 'case s a', BTW).

Um, yes.  /Mea culpa/.

> >> Try doing it with mapAccumL, which is arguably the right abstraction,
> >> but 
> >> has the components the other way around.
> > 
> > Define
> > 
> >     swap (a, b) = (b, a)
> 
> ew, that's far too crude.  I think you mean
> 
>    swap = uncurry $ flip (,)

Ah, yes.

jcc




More information about the Haskell-Cafe mailing list