Search code examples
haskellfunctor

Too many arguments for fmap


The first argument, that fmap is expected is a function with one argument.

fmap :: Functor f => (a -> b) -> f a -> f b

Then I tried as follow in prelude:

Prelude> x = fmap (\x y -> x * y)

As you can see, the first argument to fmap is a function, that has two arguments. Why does the compiler let it pass?

The function that I pass to fmap above has two arguments not one!


Solution

  • Haskell does not actually have functions with more (or less) than one argument. A "two-argument function" is really just a function that takes one argument and produces another function that takes another argument. That is, \x y -> x * y is just a syntactic short cut for \x -> \y -> x * y. This concept is known as currying.

    So this should explain what's happening in your example. Your fmap will simply turn an f of numbers into an f of functions. So, for example, x [1,2,3] would produce the list [\y -> 1 * y, \y -> 2 * y, \y -> 3 * y] (a.k.a. [(1*), (2*), (3*)]).