let choices menu = (* declare function 'choices' type of 'menu' is inferred to be 'string list list'*) let rec helper menu sofar = (*declare recursive nested function 'helper' with two arguments *) match menu with | [] -> print_string (sofar^"\n") (* match 'menu' to an empty list *) | head::tail -> (* match to a list with at least one element *) List.iter (fun item -> helper tail (sofar^item^" ")) head (* Function List.iter is called with an anonymous function as the first argument to recursively print out the menu choices *) in helper menu "" (* Invoke the helper function *);;choices [["small"; "medium"; "large"]; ["vanilla"; "ultra chocolate"; "lychee"; "rum raisin"; "ginger"]; ["cone"; "cup"]];;
The above example shows the use of pattern matching (not the same thing as regex), which is one of the most powerful features of O'Caml. A longer example would be needed to properly highlight why it's so useful.