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
List OCaml无法对列表进行排序_List_Sorting_Types_Functional Programming_Ocaml - Fatal编程技术网

List OCaml无法对列表进行排序

List OCaml无法对列表进行排序,list,sorting,types,functional-programming,ocaml,List,Sorting,Types,Functional Programming,Ocaml,我试图在Ocaml中对列表(即“a”)进行排序,但我无法。我具体使用了内置函数sort(),使用的变量类型为“int list”,因此函数内部使用的“compare module”应该没有任何问题 我的例子是: 尝试时: sort a sort [1] 引发的错误是: This expression has type int list but an expression was expected of type 'a -> 'a -> int This expression

我试图在Ocaml中对列表(即“a”)进行排序,但我无法。我具体使用了内置函数sort(),使用的变量类型为“int list”,因此函数内部使用的“compare module”应该没有任何问题


我的例子是:

尝试时:

sort a
sort [1]
引发的错误是:

This expression has type int list but an expression was expected of type 'a -> 'a -> int
This expression has type 'a List.t = 'a list but an expression was expected of type 'b -> 'b 
-> int List.t is abstract because no corresponding cmi file was found in path.
其他尝试:

尝试时:

sort a
sort [1]
引发的错误是:

This expression has type int list but an expression was expected of type 'a -> 'a -> int
This expression has type 'a List.t = 'a list but an expression was expected of type 'b -> 'b 
-> int List.t is abstract because no corresponding cmi file was found in path.

我不明白发生了什么


提前感谢。

正如Marth所说,关键是必须提供一个比较函数,以指定列表的排序方式(使用哪些标准)

对于整数的最简单情况,使用预定义的
compare
函数即可:

List.sort比较\u列表


在其他情况下(取决于目标),可以使用不同的比较函数,始终考虑要排序的列表之王。例如,我们可能需要对字符串或其他类型的列表进行排序。

您需要将比较函数作为第一个参数传递(
'a->'a->'int
)。非常感谢!那会是“排序CMPA”吗?顺便问一下,有什么好的内置整数比较函数吗?我只发现像()和merge-in()这样的实现,它们谈论的是一个cmp。对于更复杂的/自定义类型,您可能最好使用
cmp
函数(即,如果第一个值大于第二个值,则结果大于0;如果相反,则结果小于0;如果两个值相等,则结果为0(或在本比较中“视为相等”)。请注意,
compare
将适用于大多数类型,但我不会将其用于原语之外的任何东西。它有效排序比较一个已工作的“cmp”,以及一个自己的“cmp”。我将使用compare来表示原语,并为我自己的类型构造类似的函数。再次非常感谢!