This time we're going to learn about lambda expressions. F # and C # have anonymous functions as well. But it feels better to call an anonymous function a lambda.
Try writing some code:
let add = fun x y -> x + y
printfn "%A" (add 1 3)
Here we define the Add function. This definition uses an anonymous function (takes out the x,y two parameters and returns the x+y result). Fun is the key word for anonymous functions.
The function definitions that you have before are:
Let add x y = x + y
This is an elliptical form. The difference is whether the parameter is in a lambda expression.
From the experience of C #, you can pass functions as arguments to other functions.
let add = fun x y -> x + y
let hoge func =
let a = func 1 10
printfn "%A" a
hoge add // 将add作为参数传入hoge函数中
Yes, it does, the result is 11.
Of course, you can also try to pass the lambda expression directly into the previous function.
let hoge func =
let a = func 1 10
printfn "%A" a
hoge (fun x y -> x + y) // 直接传入lambda表达式
Lambda expressions are commonly used in c#3.0, which makes it less strange to use Lambda in F #.