Haskell中的运算符优先级 [英] operator precedence in Haskell

查看:147
本文介绍了Haskell中的运算符优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  *主> 2 * 3`mod` 2 
0

与此不同吗?

  *主要> 2 * mod 3 2 
2


解决方案

功能调用绑定最紧密的,所以

  2 * mod 3 2 
$ b $ p



  2 *(mod 3 2) 

请记住, mod 不是在这里用作操作符,因为没有反引号。



现在,当 mod 以中缀形式使用时,优先级为7,其中(*)也有。由于它们具有相同的优先权并且是左联合的,因此它们从左至右进行简单解析:

 (2 * 3 )`mod` 2 


I am confused about the rules for operator precedence in Haskell.
More specifically, why is this:

*Main> 2 * 3 `mod` 2
0

different than this?

*Main> 2 * mod 3 2
2

解决方案

Function calls bind the tightest, and so

2 * mod 3 2

is the same as

2 * (mod 3 2)

Keep in mind that mod is not being used as an operator here since there are no backticks.

Now, when mod is used in infix form it has a precedence of 7, which (*) also has. Since they have the same precendence, and are left-associative, they are simply parsed from left to right:

(2 * 3) `mod` 2

这篇关于Haskell中的运算符优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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