通过ocaml中的参数名称和模式匹配进行绑定

时间:2018-10-29 09:28:10

标签: pattern-matching ocaml

在OCaml中,我怎么都可以:

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

在haskell中就像

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

1 个答案:

答案 0 :(得分:7)

使用as。例如:

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

或:

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