Haskell中的运算符和函数有什么区别? [英] What is the difference between an operator and a function in Haskell?
问题描述
我是Haskell的新手,而Infix和Prefix表示法的混合使用使我感到困惑. "+"这样的运算符和head这样的函数有什么区别?如何写一个1 c 1 = 2的运算符'c'?
I am new to Haskell and this mixture of Infix and Prefix notation is confusing me. What is the difference between an operator like '+' and a function like head? How do I write an operator 'c' which does this 1 c 1 = 2?
我发现这个定义是! b =正确. Haskell怎么知道我正在定义!而不是功能吗?
I found this definition a ! b = True. How does Haskell know that I am defining ! and not a function a?
推荐答案
在Haskell中,要创建运算符,必须使用以下运算符":
In Haskell, to create an operator you must use the following "operator symbols":
! #$%* +. /< =>? \ ^ | :-〜
! # $ % * + . / < = > ? \ ^ | : - ~
例如,
($$$) a b = a+b
定义一个运算符$$$,该运算符可以在表达式1 $$$ 1中使用,以产生值2.
Defines an operator $$$ which can be used in the expression 1 $$$ 1 to yield a value of 2.
从概念上讲,运算符和函数没有什么区别,您可以使用反引号或括号来使一个像另一个一样工作.
Conceptually, there is no difference between an operator and a function, and you can use backticks or parens to make one work like the other.
就这样100%清楚了,让我演示将函数转换为运算符,反之亦然:
Just so it is 100% clear, let me demonstrate turning a function into an operator and vice versa:
对于运算符'+',以下两个表达式是等效的:
For the operator '+', the following two expressions are equivalent:
1+1
(+) 1 1
类似地,对于一个函数,以下两个表达式是等效的:
Similarly, for a function, the following two expressions are equivalent:
foo 1 2
1 `foo` 2
这篇关于Haskell中的运算符和函数有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!