Why do spaces in F # code cause errors?

I do F # Interactive.

I get strange results, but here I can not explain:

The following code returns 66, which is the expected value.

> let fx = 2*x*x-5*x+3;; > f 7;; 

The following code generates a syntax error:

 > let fx = 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 spaces between the characters in the second example.

Why does the first code example work and the second a syntax error?

+6
source share
2 answers

The error message says that you are trying to call function x with an argument of +3 (unary + by 3) and since x is not a function, therefore, This value is not a function and cannot be applied

+3
source

The problem is using +3 . When working with the +/- prefix on a numeric expression, the space is significant

  • x+3 : x plus 3
  • x +3 : syntax error: x followed by a positive value of 3

I came across this several times myself (most often with - ). At first this is a little disappointing, but in the end you will learn to recognize it.

This is not a feature without meaning. You must enable the application of negative values ​​to functions

  • myFunc x -3 : function call myFunc with parameters x and -3
+12
source

Source: https://habr.com/ru/post/897915/


All Articles