通过参数名称和模式匹配在ocaml中绑定

nic*_*las 4 ocaml pattern-matching

在OCaml中,我怎样才能:

  • 模式匹配参数
  • 并将不匹配的参数绑定到名称?

在haskell它就像

f arg@{..} = some code using both arg and its fields
Run Code Online (Sandbox Code Playgroud)

gle*_*nsl 7

使用as.例如:

let f ((a, b) as original) =
  if a > b then
    (b, a)
  else
    original
Run Code Online (Sandbox Code Playgroud)

要么:

let g = function
| [] -> []
| (x :: _) as l -> x :: l
Run Code Online (Sandbox Code Playgroud)