为什么我的 F# 代码中的空格会导致错误?
我一直在修补 F# Interactive。
我不断得到奇怪的结果,但有一个我无法解释的结果:
以下代码返回 66,这是我期望的值。
> let f x = 2*x*x-5*x+3;;
> f 7;;
以下代码引发语法错误:
> let f x = 2*x*x - 5*x +3;;
stdin(33,21): error FS0003: This value is not a function and cannot be applied
正如您所看到的,唯一的区别是第二个示例中的符号之间有一些空格。
为什么第一个代码示例可以工作,而第二个代码示例会导致语法错误?
I've been tinkering with the F# Interactive.
I keep getting weird results, but here's one I can't explain:
The following code returns 66, which is the value I expect.
> let f x = 2*x*x-5*x+3;;
> f 7;;
The following code throws a syntax error:
> let f x = 2*x*x - 5*x +3;;
stdin(33,21): error FS0003: This value is not a function and cannot be applied
As you can see, the only difference is that there are some spaces between the symbols in the second example.
Why does the first code example work while the second one results in a syntax error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误消息表明您正在尝试使用参数
+3
(unary + on 3)调用函数x
,并且由于 x 不是函数,因此该值不是函数,无法应用
The error message says that you are trying to call a function
x
with the argument+3
( unary + on 3) and since x is not a function, hence theThis value is not a function and cannot be applied
这里的问题是
+3
的使用。处理数字表达式上的+/-
前缀时,空格很重要x+3
:x 加 3x +3
:语法错误: x 后跟正值 3我自己也遇到过几次(最常见的是
-
)。一开始有点令人沮丧,但最终你会学会发现它。但这并不是一个没有意义的功能。有必要允许对函数
myFunc x -3
应用负值:使用参数x
和-3
调用函数 myFuncThe problem here is the use of
+3
. When dealing with a+/-
prefix on a number expression white space is significantx+3
: x plus 3x +3
: syntax error: x followed by the positive value 3I've run into this several times myself (most often with
-
). It's a bit frustrating at first but eventually you learn to spot it.It's not a feature without meaning though. It's necessary to allow application of negative values to functions
myFunc x -3
: call function myFunc with parametersx
and-3