views:

251

answers:

2

Is there any reason to prefer one of the following notations over the others or is this simply a matter of preference?

map toLower "FOO"

fmap toLower "FOO"

toLower <$> "FOO"

As an aside: I realize that <$> is the same as `fmap`. Am I right in the assumption that map is just a less general form of fmap?

+11  A: 

As you say, map is a less general form of fmap. If you know you have a list then I would use map as it makes the code clearer and if you make a mistake the error message is likely to be less confusing. However to a large extent it's a matter of preference.

(<$>) is the same as fmap, but isn't exported by the Prelude so not available by default - but it's easy to import from Data.Functor or Control.Applicative and is rapidly becoming a standard way to do this.

Ganesh Sittampalam
And `liftM` is the same as `fmap`, except it is overloaded on `Monad` instead of `Functor`.
Simon Marlow
`<$>` is defined (as `fmap`) in `Data.Functor`, and is re-exported by `Control.Applicative`
applicative
@applicative: thanks, I've corrected the answer.
Ganesh Sittampalam
+7  A: 

I agree with Ganesh that map is clearer for lists. I use <$> over fmap, unless it is partially applied. So I'd use fmap reverse to declare a function that reverses all elements of some functor but if I have all the arguments available (e.g. if I'm writing a line in a do block) I tend to use the operator form: reverse <$> f x

Neil Brown
I often use `fmap` in infix notation, e.g.: `reverse \`fmap\` x`, since `fmap` is in the Prelude, so that I don't have to import Control.Applicative. Then if I need partial application, I end up writing: `f = (reverse \`fmap\`)`
Tom Lokhorst