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.
Here we specify a lambda expression with "fun" as the argument to List.map
. The lambda begins with the fun
-keyword.
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]
Often we chain multiple method calls together. With the pipeline operator, we can do this without complex nesting of method calls.
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]
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.
// 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" result24 4
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 10warning 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.
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"Done
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.