Functional Programming & Proofs


Introduction (2): Functional Programming

  1. Types are inferred ... or specified
f;; //int -> int
f 2.0;; // error
let f (x:float) : float = x*(x+1.0);;
  1. Functions as parameters/results (higher-order)
let der f = fun x -> (f (x+0.01) - (f x)) / 0.01;;
// der: f: (float -> float) -> x: float -> float
let g = der f;;
g 2.0; // 5.01

Symbols can be used at the place of function 's name:

let (|>) x f = f x;; // WHAT'S THAT ?
(|>) 2.0 f;;
2.0 |> f;;

Nb. operators are also functions !

(+);; // (int -> int -> int)
(+) 1;; //int -> int

let (++) (x,y) (x2,y2) = (x+x2,y+y2);;
(1,2) ++ (3,4);; // it: int * int = (4, 6)

go further


4 - 8