简体   繁体   English

Haskell的函数应用程序运算符($)用法

[英]Haskell's function application operator ($) usage

I'm reading a piece by Bartosz Milewski wherein he defines the following function: 我正在读Bartosz Milewski撰写的一篇文章,其中他定义了以下功能:

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. 我理解这通常是为了在前缀表示形式中使用中缀函数,但我不明白为什么,在这种情况下,函数不能简单地表达为Chan (zipWith $ fs xs) ,并想知道两者之间的区别是什么。

(if you still need context, refer to the article ) (如果您仍需要上下文,请参阅文章

In this case, $ is being passed in to zipWith . 在这种情况下, $正被传递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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM