[Haskell-cafe] Re: How do you rewrite your code?

Sean Leather leather at cs.uu.nl
Wed Mar 3 16:56:36 EST 2010


>    *How do you rewrite your code to improve it?*
>

Edward Kmett just introduced one in another thread. Simplifying, it would be
this:

For all x, y, f:

do { x' <- x ; y' <- y ; return (f x' y') }
-->
f <$> x <*> y

This is a great example, because (1) it reduces clutter and "temporary"
names and (2) requires significant background knowledge on monads and
idioms. We can also generalize this to functions f with increasing arity (f
<$> x <*> y <*> z, etc.). Beginners would not get this, but once you know
this rule, it can greatly improve your coding style. Similarly with liftM2,
liftM3, etc. as mentioned by Stephen.

Any other useful tidbits to share?

Sean
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100303/67d4b897/attachment.html


More information about the Haskell-Cafe mailing list