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

Felipe Lessa felipe.lessa at gmail.com
Tue Mar 2 15:00:56 EST 2010


On Tue, Mar 02, 2010 at 08:20:30PM +0100, Sean Leather wrote:
> 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:

One handy manual transformation is trying to do more checks on
the typechecker.  GADT's + phantom types are very useful!

> 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.

Yes, they are the same, always.

--
Felipe.


More information about the Haskell-Cafe mailing list