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

查看:27
本文介绍了之间有什么区别.(点)和 $(美元符号)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

$ 运算符用于避免括号.出现在它之后的任何内容都将优先于之前出现的任何内容.

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.

回到同一个例子:

putStrLn (show (1 + 1))

  1. (1 + 1) 没有输入,因此不能与 . 运算符一起使用.
  2. show 可以接受一个 Int 并返回一个 String.
  3. putStrLn 可以接受一个 String 并返回一个 IO().
  1. (1 + 1) doesn't have an input, and therefore cannot be used with the . operator.
  2. show can take an Int and return a String.
  3. putStrLn can take a String and return an IO ().

您可以像这样将 show 链接到 putStrLn:

You can chain show to putStrLn like this:

(putStrLn . show) (1 + 1)

如果您喜欢的括号太多,请使用 $ 操作符去掉它们:

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

putStrLn . show $ 1 + 1

这篇关于之间有什么区别.(点)和 $(美元符号)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆