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

Simon Marlow marlowsd at gmail.com
Wed Mar 25 11:09:47 EDT 2009

Dan Piponi wrote:
>> Miguel Mitrofanov wrote:
>>> takeList = evalState . mapM (State . splitAt)
>> However, ironically, I stopped using them for pretty
>> much the same reason that Manlio is saying.
> Are you saying there's a problem with this implementation? It's the
> only one I could just read immediately. The trick is to see that
> evalState and State are just noise for the type inferencer so we just
> need to think about mapM splitAt. This turns a sequence of integers
> into a sequence of splitAts, each one chewing on the leftovers of the
> previous one. *Way* easier than both the zipWith one-liner and the
> explicit version. It says exactly what it means, almost in English.

But it only works out nicely because the ordering of the components of the 
pair returned by splitAt matches the ordering that the state monad expects 
(and I can never remember which way around they are in Control.Monad.State).

Try doing it with mapAccumL, which is arguably the right abstraction, but 
has the components the other way around.


More information about the Haskell-Cafe mailing list