1
votes

I have the following Haskell expression:

map ($ 5) [(-),(+),(*)]

I know the function application operator ($) applies a function to a given parameter. However since the (-), (+) and (*) functions take two parameters, by applying these functions to 5 through map, the functions are partially applied.

The resulting list will contain three functions that takes another parameter and:

(1) Subtracts the parameter from 5

(2) Adds the parameter to 5

(3) Multiplies the parameter by 5

However is it valid to say that the above expression is equivalent to the following?

[(5 -),(5 +),(5 *)]

I think this is correct, since I checked the types of (5 -), (5 +) and (5 *) in GHCI, and they are all functions that take a number and return a number:

(5 -) :: Num a => a -> a
(5 +) :: Num a => a -> a
(5 *) :: Num a => a -> a

Any insights are appreciated.

1
Yup, your explanation is correct. - duplode

1 Answers

3
votes

Correct; you can also apply the operators a second time via:

map ($4) $ map ($ 5) [(-),(+),(*)]

producing [5-4, 5 + 4, 5 * 4]

Also, you can specify the arguments to the right of the operator, giving the same result:

[((-) 5),(+ 5),(* 5)]

(The reason the (-) 5 has the "-" parenthesized, is to prevent the compiler to think you mean "minus five", a negative number, the usual interpretation of (- 5)).