如何声明一个带有2个参数并使用字母而不是符号的"运算符"?

Jam*_*man 1 haskell function

我希望能够创建VB(.net)中的运算符,如下所示

Console.WriteLine ( 16 Mod 2 )
Run Code Online (Sandbox Code Playgroud)

产生如下所示的输出

0
Run Code Online (Sandbox Code Playgroud)


如您所见,这使代码更容易阅读.我将如何制作这样做的功能?

我尝试了以下格式

equal :: Integer -> Integer -> bool
x `equal` y = x == y
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

*Main> 5 equal 5

<interactive>:1:1: error:
    * Non type-variable argument
        in the constraint: Num ((Integer -> Integer -> Bool) -> t -> t1)
      (Use FlexibleContexts to permit this)
    * When checking the inferred type
        it :: forall t t1.
              (Num ((Integer -> Integer -> Bool) -> t -> t1), Num t) =>
              t1
*Main>
Run Code Online (Sandbox Code Playgroud)

出了什么问题,我该如何正确地做到这一点?

Ben*_*Ben 7

equal当你调用它时,你需要使用反引号,就像你定义它一样.

5 `equal` 5
Run Code Online (Sandbox Code Playgroud)

你写它的方式,Haskell认为你试图equal作为一个参数传递5,而不是相反.

  • @JamesYeoman具有非符号名称的所有Haskell函数都是普通的前缀函数,而不是中缀运算符.您可以通过使用括号周围(如`(+)`)把一个管道符为前缀的功能,同样,您可以通过反引号包围它(如`\`ELEM \`转前缀功能于缀操作符`). (2认同)