OCaml equivalent to Haskell's @ in pattern matching (a.k.a. as-pattern)

Viewed 512

In Haskell, while pattern matching, I can use @ to get the entire structure in a pattern. (For easier Googling, this structure is known as an as-pattern.)

For example, x:xs decomposes a list into a head and a tail; I can also get the entire list with xxs@(x:xs).

Does OCaml have something like this?

2 Answers

You can use as:

let f = function
| [] -> (* ... *)
| (x::xs) as l ->
  (*
   here:
   - x is the head
   - xs is the tail
   - l is the whole list
  *)

Let me extend Etienne's answer a little bit with some examples:

let x :: xs as list = [1;2;3];;
val x : int = 1
val xs : int list = [2; 3]
val list : int list = [1; 2; 3]

When you write <pattern> as <name>, the variable <name> is bound to the whole pattern on the left, in other words, the scope of as extends as far to the left as possible (speaking more techically as has lower priority than constructors, i.e., the constructors bind tighter). So, in case of the deep pattern matching, you might need to use parentheses to limit the scope, e.g.,

let [x;y] as fst :: ([z] as snd) :: xs as list = [[1;2];[3]; [4]];;
val x : int = 1
val y : int = 2
val fst : int list = [1; 2]
val z : int = 3
val snd : int list = [3]
val xs : int list list = [[4]]
val list : int list list = [[1; 2]; [3]; [4]]
Related