Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting OCaml合并排序函数_Sorting_Functional Programming_Ocaml - Fatal编程技术网

Sorting OCaml合并排序函数

Sorting OCaml合并排序函数,sorting,functional-programming,ocaml,Sorting,Functional Programming,Ocaml,这是我在OCaml中使用的合并排序函数。有趣的是,代码提供了我所期望的,也就是说,它对列表进行排序。但这也会引起一些错误。有人能检查一下我的代码,告诉我发生了什么,为什么会出现这些错误吗?我如何消除它们?我是一个OCaml新手,但我真的很想知道发生了什么: (* Merge Sort *) (* This works but produces some extra error. Consult someone!! *) let rec length_inner l n = match l

这是我在OCaml中使用的合并排序函数。有趣的是,代码提供了我所期望的,也就是说,它对列表进行排序。但这也会引起一些错误。有人能检查一下我的代码,告诉我发生了什么,为什么会出现这些错误吗?我如何消除它们?我是一个OCaml新手,但我真的很想知道发生了什么:

(* Merge Sort *)
(* This works but produces some extra error. Consult someone!! *)
let rec length_inner l n =
    match l with
    [] -> n
    | h::t -> length_inner t (n + 1)
;;

let length l = length_inner l 0;;

let rec take n l =
    if n = 0 then [] else
        match l with
        h::t -> h :: take (n - 1) t
;;

let rec drop n l =
    if n = 0 then l else
        match l with
        h::t -> drop (n - 1) t
;;

let rec merge x y =
    match x, y with 
    [], l -> l
    | l, [] -> l
    | hx::tx, hy::ty -> 
        if hx < hy
            then hx  :: merge tx (hy :: ty)
    else hy :: merge (hx :: tx) ty
;;

let rec msort l =
    match l with
    [] -> []
    | [x] -> [x]
    | _ ->
        let left = take (length l/2) l in 
        let right = drop (length l/2) l in
        merge (msort left) (msort right)
;;

msort [53; 9; 2; 6; 19];; 
(*合并排序*)
(*这可以工作,但会产生一些额外的错误。请咨询他人!!*)
让rec length_inner l n=
匹配
[]->n
|h::t->长度\内部t(n+1)
;;
设长度l=长度_内部l 0;;
让rec拿n l=
如果n=0,则[]否则
匹配
h::t->h::take(n-1)t
;;
让rec放下n l=
如果n=0,则l=0
匹配
h::t->下降(n-1)t
;;
让rec合并x和y=
将x,y与
[],l->l
|l,[]->l
|hx::tx,hy::ty->
如果hx []
|[x]->[x]
| _ ->
让左=取(长度l/2)l
让右=下降(长度l/2)l英寸
合并(msort左)(msort右)
;;
msort[53;9;2;6;19];;
在终端,我得到:

        OCaml version 4.00.1

# #use "prac.ml";;
val length_inner : 'a list -> int -> int = <fun>
val length : 'a list -> int = <fun>
File "prac.ml", line 13, characters 2-44:
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
[]
val take : int -> 'a list -> 'a list = <fun>
File "prac.ml", line 19, characters 2-39:
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
[]
val drop : int -> 'a list -> 'a list = <fun>
val merge : 'a list -> 'a list -> 'a list = <fun>
val msort : 'a list -> 'a list = <fun>
- : int list = [2; 6; 9; 19; 53]
# 
OCaml版本4.00.1
##使用“prac.ml”;;
val length_INERNAR:'a list->int->int=
val length:'a list->int=
文件“prac.ml”,第13行,字符2-44:
警告8:此模式匹配并非详尽无遗。
以下是一个不匹配值的示例:
[]
val take:int->“a列表->”a列表=
文件“prac.ml”,第19行,字符2-39:
警告8:此模式匹配并非详尽无遗。
以下是一个不匹配值的示例:
[]
val drop:int->“a列表->”a列表=
val merge:'a list->'a list->'a list=
val msort:'列表->'列表=
-:int list=[2;6;9;19;53]
# 

编译器告诉您,您的模式匹配并非详尽无遗。事实上,它确切地告诉了我们如何看待这个问题。例如,您可以尝试:

drop 2 []
要解决此问题,您需要决定如何处理函数中的空列表。下面是一个包含详尽匹配的drop定义:

let rec drop n l =
    if n = 0 then l
    else
        match l with
        | [] -> []
        | h::t -> drop (n - 1) t
如果这不清楚:您的代码没有说明如何处理空列表。如果列表的格式为
h::t
,则匹配项仅说明要执行的操作。但空列表没有此表单。您需要将
[]
的案例添加到匹配项中