Haskell中缀函数应用优先

Haskell中缀函数应用优先

本文介绍了Haskell中缀函数应用优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

f x y = x * y 。我们可以用两种方法来应用这个函数: f 5 6 ,或者,使用中缀表示法, 5`f` 6 。操作员规则是否适用于最后一个表达式?这个应用程序有什么优先权?它只是函数应用程序的另一种形式,它也会具有最高的优先级?



我想编译器会看到这种特殊的形式(由于``和/或以字母(?)开头的名称),并将其视为普通函数应用程序,而不是将其视为运算符。

解决方案

有一个将其清除:

正如其他答案所表明的那样,报告还有一节关于,允许您定义自己的固定性,例如:

  infixl 7`op` 


Let f x y = x * y. We can apply this function in two ways: f 5 6, or, using infix notation, 5 `f` 6. Do the operator rules apply to this last expression? What precedence will this application have? Is it just another form of function application, and so will it also have the highest precedence?

I suppose that the compiler sees this special form (due to `` and/or the name starting with a letter(?)), and actually treats this as ordinary function application, instead of considering it an operator.

解决方案

The Haskell 98 Report has a section on Operator Applications that clears it up:

As indicated by the other answers, the Report also has a section on Fixity Declarations that allows you to define your own fixity, for example:

infixl 7 `op`

这篇关于Haskell中缀函数应用优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 12:47