问题 如何给lambda中的运算符提供不可用性?


例如,这不是类型检查

\cons nil -> 5 `cons` 3 `cons` nil

这也不是

\(#) -> 5 # 3 # nil

虽然这两者都有

\cons nil -> 5 `cons` nil
\(#) nil -> 5 # nil

有没有办法在lambdas中为运营商分配infixites。我试过了

infixr 5 #
foo = \(#) nil -> 5 # 3 # nil

这给出了没有定义的错误 # 和

foo = \(infixr 5 #) nil -> 5 # 3 # nil

这只是一个语法错误。

我能做什么?


1786
2017-12-05 18:14


起源



答案:


固定声明可以是本地的,但必须伴随定义,因此您必须编写类似的内容

foo cons nil = 'a' # 'b' # nil
  where (#) = cons
        infixr 5 #

要么

foo = \cons nil -> let (#) = cons; infixr 5 # in 'a' # 'b' # nil

等等


16
2017-12-05 18:45



确实很好!这是Haskell98,Haskell2010或GHC的一部分吗? - leftaroundabout
我特别喜欢let版本。 - PyRulez
它是Haskell 98. let和where的语法涉及“decls”,其中包括类型签名,固定声明以及函数和模式绑定。 - Reid Barton
请不要写 let 版 - jberryman
@jberryman它有什么问题? - PyRulez


答案:


固定声明可以是本地的,但必须伴随定义,因此您必须编写类似的内容

foo cons nil = 'a' # 'b' # nil
  where (#) = cons
        infixr 5 #

要么

foo = \cons nil -> let (#) = cons; infixr 5 # in 'a' # 'b' # nil

等等


16
2017-12-05 18:45



确实很好!这是Haskell98,Haskell2010或GHC的一部分吗? - leftaroundabout
我特别喜欢let版本。 - PyRulez
它是Haskell 98. let和where的语法涉及“decls”,其中包括类型签名,固定声明以及函数和模式绑定。 - Reid Barton
请不要写 let 版 - jberryman
@jberryman它有什么问题? - PyRulez