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 )
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.