# let haar l =
let rec aux l s d =
match l, s, d with
[s], [], d -> s :: d
| [], s, d -> aux s [] d
| h1 :: h2 :: t, s, d -> aux t (h1 + h2 :: s) (h1 - h2 :: d)
| _ -> invalid_arg "haar"
in aux l [] [];;
val haar : int list -> int list = <fun>
# haar [1; 2; 3; 4; -4; -3; -2; -1];;
- : int list = [0; 20; 4; 4; -1; -1; -1; -1]
Pattern matching allows complicated transformations to be represented clearly and succinctly. Moreover, the Caml compiler turns pattern matches into very efficient code, at times resulting in programs that are shorter and faster than equivalent code written with a case statement (Cardelli 1984, p. 210.).