OCaml Pattern Matching Defining a function using pattern matching


Example

The keyword function can be used to initiate pattern-matching on the the last argument of a function. For example, we can write a function called sum, which computes the sum of a list of integers, this way

let rec sum = function
  | [] -> 0
  | h::t -> h + sum t
;;

val sum : int list -> int = <fun>