There are numerous threads on the Haskell Café involving rewriting, refactoring, refining, and in general improving code (for some definition of improve). I am interested in seeing examples of how Haskell code can be rewritten to make it better. Some general examples are:

My question is simple:

   How do you rewrite your code to improve it?

You can answer this in any way you like, but I think the most useful answer is to show a reasonably small, concrete example of what your code looked like before and after. Also, please describe how you think the rewrite improves such code.

Here is an example that I find myself doing occasionally.

For all x, f:

x >>= return . f
-->
fmap f x
or
f <$> x -- requires importing Control.Applicative

I think the right-hand side (RHS) is more concise and simpler. The types here do change: the type constructor has a Monad constraint in the left-hand side and a Functor constraint in the RHS. Types that are Monad instances are generally also Functor instances, so this is often possible. I'm convinced the semantics are preserved, though I haven't proven it.

What's an example of a rewrite that you've encountered?

Thanks,
Sean