OCaml Pattern Matching Defining a function using pattern matching

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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>


Got any OCaml Question?