Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass functions in F#

Is it possible to pass a reference to a function to another function in F#? Specifically, I'd like to pass lambda functions like

foo(fun x -> x ** 3)

More specifically, I need to know how I would refer to the passed function in a function that I wrote myself.

like image 525
pschorf Avatar asked Sep 04 '08 15:09

pschorf


2 Answers

Yes, it is possible. The manual has this example:

> List.map (fun x -> x % 2 = 0) [1 .. 5];;

val it : bool list
= [false; true; false; true; false]
like image 127
Mark Cidade Avatar answered Oct 20 '22 09:10

Mark Cidade


Functions are first class citizens in F#. You can therefore pass them around just like you want to.

If you have a function like this:

let myFunction f =
    f 1 2 3

and f is function then the return value of myFunction is f applied to 1,2 and 3.

like image 37
nickd Avatar answered Oct 20 '22 11:10

nickd