8

I'm reading a piece by Bartosz Milewski wherein he defines the following function:

instance Applicative Chan where
  pure x = Chan (repeat x)
  (Chan fs) <*> (Chan xs) = Chan (zipWith ($) fs xs)

Why is the function application operator in parenthesis? I understand this is normally done in order to use an infix function in prefix notation form, but I don't understand why, in this case, the function couldn't couldn't simply be expressed as Chan (zipWith $ fs xs), and wonder what the difference between the two is.

(if you still need context, refer to the article)

2
  • 1
    Would it be any simpler if it was written as Chan (zipWith id fs xs)? It's exactly the same as the current implementation. Commented Jan 9, 2015 at 21:40
  • Remember that ($) isn't some magical primitive operator: it's a function just like (+) and or. Commented Jan 10, 2015 at 15:06

1 Answer 1

14

In this case, $ is being passed in to zipWith. It's the same as writing

zipWith (\ f x ->  f x) fs xs

Without parentheses, it would have been equivalent to

zipWith (fs xs)

which is not going to typecheck.

An operator in parentheses behaves exactly like a normal identifier. With the following definition:

apply = ($)

the code could have looked like

zipWith apply fs xs
Sign up to request clarification or add additional context in comments.

1 Comment

Specifically, the grammar of haskell requires you to say zipWith <expr>, where <expr> is an expression. ($) is the infix operator $ converted to an expression, which is a bit different than "converted to prefix", and allows both usage as a prefix function and usage as an argument to other functions.

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.