3

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
  • 2
    Yup, your explanation is correct. Commented Jun 14, 2019 at 1:36

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.