简体   繁体   English

Lambda 表示法 function 签名不能在 ghc 中编译,但在解释器 (ghci) 中可以

[英]Lambda notation function signature does not compile in ghc, but OK in interpreter (ghci)

Lambda notation function signature does not compile in ghc, but OK in interpreter (ghci) I am using stack on mac osx. Lambda 表示法 function 签名不能在 ghc 中编译,但在解释器(ghci)中可以,我在 mac osx 上使用堆栈。 ghc version is 8.8.3 ghc 版本是 8.8.3

\x y -> 2*x + y :: Num a => a -> a -> a 
valNum1 = \x y -> 2*x + y 

The problem is that you define a signature for the lambda expression, but this lambda expression is an expression , not a declaration.问题是您为 lambda 表达式定义了一个签名,但是这个 lambda 表达式是一个表达式,而不是一个声明。 If you want to specify the signature, then you specify this as the signature of valNum1 :如果要指定签名,则将其指定为valNum1的签名:

valNum1 :: Num a => a -> a -> a
valNum1 = \x y -> 2*x + y

It is however more convenient to specify the variables in the head of valNum1 :然而,在valNum1的头部指定变量更方便:

valNum1 :: Num a => a -> a -> a
valNum1 x y = 2*x + y

or you can write this as a point-free variant:或者您可以将其编写为无点变体:

valNum1 :: Num a => a -> a -> a
valNum1 = (+) . (2 *)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM