You can write a function of type apply that takes any F # function and an array of arguments and calls the function through Reflection.
However, there is a good reason why this is not part of the standard F # library - one of the strengths of F # (compared, for example, Mathematica) is that it is a statically typed language and can capture most potential errors during compilation. If you use something like apply , you will lose this check [because you never know if the list has the correct length].
However, here is an example of how to do this:
open Microsoft.FSharp.Reflection let apply (f:obj) (args:obj[]) = if FSharpType.IsFunction(f.GetType()) then let invoke = f.GetType().GetMethods() |> Seq.find (fun mi -> mi.Name = "Invoke" && mi.GetParameters().Length = args.Length) invoke.Invoke(f, args) else failwith "Not a function"
An example of use looks like this:
let add ab = a + b;; apply add [| 3;4 |];;
source share