Should functions not be the parameter?
let sub (a:float[]) (b:float[])=
[| for i = 0 to Array.length a - 1 do yield a.[i]-b.[i] |]
let fx t = [0..t]
let x=sub (fx t) (fx t)
Above compiles OK.
But the same causes an error when I replace the function call with a method invocation:
type type_f = delegate of float -> float[]
let mutable f =new type_f(fun t->[|0..t|])
let m=f.Invoke(5) //float[]
let y=sub m f.Invoke(5)//error
How do I pass the result of a method invocation as a parame开发者_StackOverflow中文版ter to a function?
In F#, the line
let y = sub m f.Invoke(5)
ambiguously parses as
let y = sub m f.Invoke (5)
(passing three arguments to sub
).
To fix this, you need more parentheses:
let y = sub m (f.Invoke(5))
(Note that using delegate
types is not idiomatic unless interoperating with .NET code in other languages. If f
was a function, then
let y = sub m (f 5)
would be sufficient, as you have noted.)
精彩评论