The $
operator is for avoiding parentheses.
($
运算符用于避免括号。)
Anything appearing after it will take precedence over anything that comes before. (之后出现的所有内容将优先于之前出现的所有内容。)
For example, let's say you've got a line that reads:
(例如,假设您有一行显示:)
putStrLn (show (1 + 1))
If you want to get rid of those parentheses, any of the following lines would also do the same thing:
(如果要删除这些括号,则以下任何一行都将执行相同的操作:)
putStrLn (show $ 1 + 1)
putStrLn $ show (1 + 1)
putStrLn $ show $ 1 + 1
The primary purpose of the .
(的主要目的.
)
operator is not to avoid parentheses, but to chain functions. (运算符不是要避免括号,而是要链接函数。)
It lets you tie the output of whatever appears on the right to the input of whatever appears on the left. (它使您可以将右侧显示的输出与左侧显示的输入绑定在一起。)
This usually also results in fewer parentheses, but works differently. (通常,这也会导致括号较少,但工作方式有所不同。)
Going back to the same example:
(回到同一个例子:)
putStrLn (show (1 + 1))
-
(1 + 1)
doesn't have an input, and therefore cannot be used with the .
((1 + 1)
没有输入,因此不能与一起使用.
)
operator. (操作员。)
-
show
can take an Int
and return a String
. (show
可以接受一个Int
并返回一个String
。)
-
putStrLn
can take a String
and return an IO ()
. (putStrLn
可以使用String
并返回IO ()
。)
You can chain show
to putStrLn
like this:
(您可以像这样将show
到putStrLn
:)
(putStrLn . show) (1 + 1)
If that's too many parentheses for your liking, get rid of them with the $
operator:
(如果您喜欢的括号太多,请使用$
运算符将其删除:)
putStrLn . show $ 1 + 1
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…