Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
606 views
in Technique[技术] by (71.8m points)

haskell - 之间有什么区别。 (点)和$(美元符号)?(What is the difference between . (dot) and $ (dollar sign)?)

What is the difference between the dot (.) and the dollar sign ($) ?

(点(.)和美元符号($)什么区别?)

As I understand it, they are both syntactic sugar for not needing to use parentheses.

(据我了解,它们都是不需要使用括号的语法糖。)

  ask by Rabarberski translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 + 1) doesn't have an input, and therefore cannot be used with the .

    ((1 + 1)没有输入,因此不能与一起使用.)

    operator.

    (操作员。)

  2. show can take an Int and return a String .

    (show可以接受一个Int并返回一个String 。)

  3. putStrLn can take a String and return an IO () .

    (putStrLn可以使用String并返回IO () 。)

You can chain show to putStrLn like this:

(您可以像这样将showputStrLn :)

(putStrLn . show) (1 + 1)

If that's too many parentheses for your liking, get rid of them with the $ operator:

(如果您喜欢的括号太多,请使用$运算符将其删除:)

putStrLn . show $ 1 + 1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...