Std\Function
Function combinators — identity, composition, application, flipping.
These are the fundamental higher-order function building blocks.
Functions
Section titled “Functions”identity : Int -> Int
Section titled “identity : Int -> Int”Returns its argument unchanged.
identity 42 # => 42const : Int -> Int -> Int
Section titled “const : Int -> Int -> Int”Creates a function that always returns value, ignoring its argument.
let always5 = const 5 in always5 99 # => 5compose : (a -> b) -> (c -> d) -> Int -> Int
Section titled “compose : (a -> b) -> (c -> d) -> Int -> Int”Composes two functions: (compose f g) x = f (g x).
let double = \x -> x * 2 inlet inc = \x -> x + 1 incompose double inc 3 # => 8 (double(inc(3)) = double(4) = 8)flip : (a -> b) -> Int -> Int -> Int
Section titled “flip : (a -> b) -> Int -> Int -> Int”Swaps the arguments of a two-argument function.
let sub = \a b -> a - b inflip sub 3 10 # => 7 (sub 10 3)on : (a -> b) -> (c -> d) -> Int -> Int -> Int
Section titled “on : (a -> b) -> (c -> d) -> Int -> Int -> Int”Applies a function to both arguments before combining.
(on cmp f) a b = cmp (f a) (f b)
let compareLength = on (\a b -> a - b) (\s -> length s) incompareLength [1,2,3] [1,2] # => 1apply : Int -> (a -> b) -> Int
Section titled “apply : Int -> (a -> b) -> Int”Applies a function to a value (flip of function application).
apply 42 (\x -> x + 1) # => 43pipe : Int -> [a] -> Int
Section titled “pipe : Int -> [a] -> Int”Pipes a value through a chain of functions (left to right).
pipe x [f, g, h] = h (g (f x))
let fns = [\x -> x + 1, \x -> x * 2, \x -> x - 3] inpipe 5 fns # => 9 ((5+1)*2-3 = 9)fix : (a -> b) -> Int
Section titled “fix : (a -> b) -> Int”Fixed-point combinator for anonymous recursion.
fix f = f (fix f) — enables recursion without naming.
let factorial = fix (\self n -> if n <= 1 then 1 else n * (self (n - 1))) infactorial 5 # => 120