Begtostudy
Begtostudy

Reputation: 1424

Why doesn't my code compile?

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])

Upvotes: 3

Views: 509

Answers (3)

Yin Zhu
Yin Zhu

Reputation: 17119

The format of your list/array comprehension is wrong.

you either use -> as a short cut:

let a = [1;2;3]
[| for i in a -> i |]

or formally write yield:

[| for i in a do yield i |]

Upvotes: 1

Brian
Brian

Reputation: 118925

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.

Upvotes: 7

ChaosPandion
ChaosPandion

Reputation: 78292

This compiles:

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

Upvotes: 3

Related Questions