fun Keyword Examples
This page was last reviewed on Nov 21, 2023.
Dot Net Perls
Fun, lambdas. A function is an object. In F# we use the fun-keyword to create lambda expressions. This is powerful—we can pass these functions as arguments.
With map and filter, methods on Lists, we can apply transformations with functions. We can pipeline these calls for simpler syntax. A pipeline operator is available.
First example. Here we specify a lambda expression with "fun" as the argument to List.map. The lambda begins with the fun-keyword.
Info On the left side of the lambda, after "fun" we have the argument to the lambda. And the right side is the expression that is returned.
Here We use List.map to multiply each element in the list by 2. We print the before and after lists.
let values = [10; 20; 30; 40; 50] // Use List.map with a lambda expression. // ... The lambda doubles each element. let result = List.map (fun x -> x * 2) values // Print our before and after lists. printfn "%A" values printfn "%A" result
[10; 20; 30; 40; 50] [20; 40; 60; 80; 100]
Pipeline. Often we chain multiple method calls together. With the pipeline operator, we can do this without complex nesting of method calls.
Detail We call one method after another, using the return value from the first as the argument to the second.
Here We use List.filter to eliminate values less than 3. Then we multiply all remaining values by 2.
let values = [1; 2; 3; 4; 5] // Use filter to get only elements greater than or equal to 3. // ... Use map to double their values. // Use pipeline operator. let result = values |> List.filter (fun v -> v >= 3) |> List.map (fun x -> x * 2) // Print lists. printfn "%A" values printfn "%A" result
[1; 2; 3; 4; 5] [6; 8; 10]
Function composition. With these operators we call two functions, one before the other. Aright-pointing arrow calls the functions from left to right. Left arrows go from right to left.
Here We have two functions, one that divides by 3 and one that adds 1. These are called in different orders (with different arguments).
So Order matters. Unlike in a pipeline, the same argument is passed to both functions.
// Some functions. let divideNumber x = x / 3 let addOneToNumber n = n + 1 // Use function composition, left to right. // ... This divides then adds (so equals 4). let composition1 = divideNumber >> addOneToNumber let result1 = composition1 9 printfn "%A" result1 // Use function composition, right to left. // ... This adds then divides (so equals 4). let composition2 = divideNumber << addOneToNumber let result2 = composition2 11 printfn "%A" result2
4 4
Expression type error. The F# compiler issues a warning when an expression has a return value that is discarded. We use the keyword ignore to specify this is expected.
let multiplyNumbers left right = left * right // This causes a warning. multiplyNumbers 5 10
warning FS0020: This expression should have type 'unit', but has type 'int'. Use 'ignore' to discard the result of the expression, or 'let' to bind the result to a name.
Ignore keyword. Here we use "ignore" to specify that we want the result of an expression to be discarded. The F# compiler issues no warning on this program. And the function has no effect.
// This function computes a value. let multiplyNumbers left right = left * right // Ignore the result of multiplyNumbers. ignore (multiplyNumbers 5 10) // Program is done. printfn "Done"
Summary. Higher-order procedures are a key part to F# programming. These lead to clearer, less bug-prone code. We describe logic at a higher level.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 21, 2023 (simplify).
© 2007-2024 Sam Allen.