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
Algorithm 使用fold_left和bool函数作为参数传递的插入排序_Algorithm_Sorting_Ocaml - Fatal编程技术网

Algorithm 使用fold_left和bool函数作为参数传递的插入排序

Algorithm 使用fold_left和bool函数作为参数传递的插入排序,algorithm,sorting,ocaml,Algorithm,Sorting,Ocaml,我想用fold_left编写简单的插入排序函数,但我也想传递一个函数,该函数将在排序过程中指定顺序。 我不知道的是,如何把它传给左折 let rec insert f l e = match l with | [] -> [e] | h :: t -> if f e h then h :: insert f t e else e :: l;; let insertion_sort f l = List.fold_left insert f [] l;; let

我想用fold_left编写简单的插入排序函数,但我也想传递一个函数,该函数将在排序过程中指定顺序。 我不知道的是,如何把它传给左折

let rec insert f l e = 
match l with
    | [] -> [e]
    | h :: t -> if f e h then h :: insert f t e else e :: l;;

let insertion_sort f l = List.fold_left insert f [] l;;

let less x y = x < y;;

let result = insertion_sort less  [2 ; 5 ; 1 ; 9 ; 7 ; -2 ; 0 ; 124];;
List.fold_左插入f。。。将insert和f作为单独的参数应用于List.fold_left。您需要的是List.fold insert f…,它会将f应用于insert,然后将结果应用于List.fold_left


编辑:此外,您不需要定义更少。您可以将>作为函数直接传递,方法是将其括在括号中:insertion\u sort Thx。。。。我打赌我曾经尝试过这一个,但可能我有其他错误,只是没有再尝试这个解决方案。。
let insertLess = insert less;;

let insertion_sortLess l = List.fold_left insertLess [] l;;

let result = insertion_sortLess [2 ; 5 ; 1 ; 9 ; 7 ; -2 ; 0 ; 124];;
#   val result : int list = [124; 9; 7; 5; 2; 1; 0; -2]