Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does whitespace in my F# code cause errors?

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?

like image 532
Vivian River Avatar asked Sep 23 '11 16:09

Vivian River


2 Answers

The problem here is the use of +3. When dealing with a +/- prefix on a number expression white space is significant

  • x+3: x plus 3
  • x +3: syntax error: x followed by the positive value 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
like image 189
JaredPar Avatar answered Nov 15 '22 10:11

JaredPar


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 the This value is not a function and cannot be applied

like image 21
manojlds Avatar answered Nov 15 '22 10:11

manojlds