Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell's function application operator ($) usage

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)

like image 618
Brendan Avatar asked Jan 09 '15 21:01

Brendan


1 Answers

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
like image 92
Tikhon Jelvis Avatar answered Oct 11 '22 09:10

Tikhon Jelvis