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

Simon Marlow marlowsd at gmail.com
Wed Mar 25 11:32:23 EDT 2009


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!  (and you have the a and s the 
wrong way around in 'case s a', BTW).

>> 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 (,)

Cheers,
	Simon


More information about the Haskell-Cafe mailing list