OCaml Factorial Function using Pattern Matching


Example

let rec factorial n = match n with
| 0 | 1 -> 1
| n -> n * (factorial (n - 1))

This function matches on both the values 0 and 1 and maps them to the base case of our recursive definition. Then all other numbers map to the recursive call of this function.