F# Functions

First-class functions and functional composition

Function Basics

// Simple function
let add x y = x + y

// Type signature: int -> int -> int
let result = add 3 5  // result = 8

Function Composition

Pipe Operator

let square x = x * x
let addOne x = x + 1

5 |> square |> addOne  // 26

Composition

let squareThenAddOne = 
  square >> addOne

squareThenAddOne 5  // 26

Higher-Order Functions

// Function taking another function
let applyTwice f x = f (f x)

applyTwice square 3  // 81 (3 → 9 → 81)