Free preview.You're sampling one lesson — enroll free to unlock all 10 lessons and track your progress.
Enroll free lesson
Closures and Higher-Order Functions
Closures and Higher-Order Functions
In this lesson — part of Advanced Features — you'll learn closures and higher-order functions in F# and why it matters in real work.
Why it matters
Functions that take or return functions unlock concise, composable code.
Key ideas
- Functions as values
- map / filter / reduce
- Callbacks
- Composition
In practice
Here's how it looks in idiomatic F#:
// Functions taking/returning functions are everywhere
let applyTwice f x = f (f x)
printfn "%d" (applyTwice (fun n -> n * 3) 2) // 18
// The core list combinators are higher-order
let evens = [1..10] |> List.filter (fun n -> n % 2 = 0)
let names = ["ada"; "bob"] |> List.map (fun s -> s.ToUpper())
// Returning a function (a closure-based adder factory)
let adder n = fun x -> x + n
printfn "%A %A %d" evens names (adder 100 1)
F# note: Currying means there's no real distinction between 'a 2-arg function' and 'a function returning a function', so higher-order composition with map/filter/fold is the default style, not an advanced technique.
Try it yourself
Exercise: In F#, use map and filter to get the squares of the even numbers.
Recap
You now understand closures and higher-order functions and can apply it in F#. Mark this lesson complete and continue to the next one.
