Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't my code compile?

Tags:

f#

let sub (m:double[],n:double[]) : double[]=
    [| for i = 0 to Array.length m -1 do m.[i]-n.[i] |]

Error 1 This value is not a function and cannot be applied E:\MyDocuments\Visual Studio 2010\Projects\curve intersection\newton\Module1.fs 27 21 newton

But, this is ok:

let a = [| "a"; "b"; "c"; "d"; "e"; "f" |]

for i = 0 to Array.length a - 1 do
    System.Console.WriteLine(a.[i])
like image 667
Begtostudy Avatar asked Nov 21 '25 17:11

Begtostudy


2 Answers

Spaces around a minus sign matter:

f -1   // means f(-1)

calls the function f with an argument of -1 (unary minus). Whereas

n - 1

and

n-1

are subtraction.

The compiler error reflects that

Array.length m -1

parses as

(Array.length m)(-1)

as though it is expecting the first expression to return a function, which will then be applied to the value -1. Since length actually returns an int, you get the error message that says that an integer is not a function and cannot be applied to the argument -1.

like image 130
Brian Avatar answered Nov 24 '25 22:11

Brian


This compiles:

let sub (m:double[], n:double[]) : double[] =
    [| for i = 0 to Array.length m - 1 do yield m.[i] - n.[i] |]
like image 43
ChaosPandion Avatar answered Nov 24 '25 23:11

ChaosPandion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!