用于将两个排序列表合并为一个排序列表的函数的OCaml样式

用于将两个排序列表合并为一个排序列表的函数的OCaml样式,ocaml,Ocaml,我是OCaml新手,正在审核一个类。我有一个作业提示,上面写着: “merge xs ys接受两个整数列表,每个列表按递增顺序排序, 并按排序顺序返回单个合并列表。“ 我已成功编写了一个有效的函数: let rec merge xs ys = match xs with | [] -> ys | hxs::txs -> if hxs <= (match ys with | [] -> hxs | hys::tys -> hys)

我是OCaml新手,正在审核一个类。我有一个作业提示,上面写着: “merge xs ys接受两个整数列表,每个列表按递增顺序排序, 并按排序顺序返回单个合并列表。“

我已成功编写了一个有效的函数:

let rec merge xs ys = match xs with
  | [] -> ys
  | hxs::txs -> if hxs <= (match ys with
    | [] -> hxs
    | hys::tys -> hys)
      then hxs :: merge txs ys 
      else match ys with
      | [] -> xs
      | hys::tys -> hys :: merge xs tys  in
merge [-1;2;3;100] [-1;5;1001]
;;
让rec merge xs ys=将xs与
|[]->ys
|换热器::换热器->换热器换热器
|hys::tys->hys)
然后hxs::合并txs ys
还有什么能与之相匹配的吗
|[]->xs
|hys::tys->hys::在中合并xs tys
合并[-1;2;3;100][-1;5;1001]
;;
我想知道我的代码是否被认为是可以接受的OCaml风格?我想避免养成任何坏习惯。它的构图很密集,但可能是因为我还不习惯OCaml


谢谢。

我个人觉得很难理解
如果hxs想耍聪明,你也可以将前两种情况表述为单个
|([],rest)|(rest,[])->rest
。非常感谢。我觉得一定有办法重构我使用的所有匹配项。这是一个很好的例子。
 ...
 let hys =
   match ys with
   | [] -> hxs
   | hys :: _ -> hys
 in
 if hxs < hys then
    hxs :: merge txs ys
 ...
let rec merge xs ys =
   match xs, ys with
   | [], _ -> ys
   | _, [] -> xs
   | hx :: txs, hy :: tys ->
       if hx < hy then hx :: merge txs ys else hy :: merge xs tys