简体   繁体   English

为什么我的F#代码中的空格会导致错误?

[英]Why does whitespace in my F# code cause errors?

I've been tinkering with the F# Interactive. 我一直在修改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. 以下代码返回66,这是我期望的值。

> 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? 为什么第一个代码示例工作而第二个代码示例导致语法错误?

The problem here is the use of +3 . 这里的问题是+3的使用。 When dealing with a +/- prefix on a number expression white space is significant 处理数字表达式上的+/-前缀时,空格很重要

  • x+3 : x plus 3 x+3 :x加3
  • x +3 : syntax error: x followed by the positive value 3 x +3 :语法错误:x后跟正值3

I'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 parameters x and -3 myFunc x -3 :使用参数x-3调用函数myFunc

错误消息表明您正在尝试使用参数+3 (一元+表示3)调用函数x ,并且由于x不是函数,因此This value is not a function and cannot be applied函数,因此This value is not a function and cannot be applied

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

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