binding via argument name and pattern match in ocaml

Viewed 87

In OCaml, how can I both :

  • pattern match on an argument
  • and also bind the unmatched argument to a name ?

in haskell it is like

f arg@{..} = some code using both arg and its fields
1 Answers

Use as. E.g.:

let f ((a, b) as original) =
  if a > b then
    (b, a)
  else
    original

or:

let g = function
| [] -> []
| (x :: _) as l -> x :: l
Related