f-sharp
  1. f-sharp-lambda-expression

F# Lambda Expression

Lambda expressions in F# are functions without a name or identifier. They are anonymous functions that can accept zero or more parameters. Lambda expression is a concise way to express function definition.

Syntax

fun <parameter-list> -> <function-body>

Example

let add = fun a b -> a + b
printfn "%d" (add 10 20)

Output

30

Explanation

In the above example code, a lambda expression is defined which takes two parameters a and b and adds them together to get the result. The lambda expression is then saved to the variable add. Finally, the add function is called with arguments 10 and 20, and then the result is printed to the console.

Use

Lambda expressions in F# can be used anywhere a regular function is used. They are commonly used as arguments to higher-order functions such as List.map and Seq.filter.

Important Points

  • Lambda expressions are functions without a name or identifier.
  • They can accept zero or more parameters.
  • They are defined using the fun keyword followed by the parameter list and function body.
  • Lambda expressions are commonly used as arguments to higher-order functions.

Summary

In F# programming language, a lambda expression is a concise way to express a function definition. They are commonly used as arguments to higher-order functions such as List.map and Seq.filter. Lambda expressions are defined using the fun keyword followed by the parameter list and function body.

Published on: