【发布时间】:2017-03-27 18:34:11
【问题描述】:
我上周学习了 SML,所以我尝试编写 Quicksort 代码以很好地理解它。 我的代码是:
fun quicksort(l: int list) =
if length l < 2
then l
else let fun split(l:int list, v: int) =
if null l
then ([], [])
else if v > hd l
then ((hd l)::(#1 split(tl l, v)), #2 split(tl l, v))
else (#1 split(tl l, v), (hd l)::(#2 split(tl l, v)))
in
(#1 split(quicksort(tl l), hd l)) @ ((hd l)::(#2 split(quicksort(tl l), hd l)))
end
这是错误信息:
Standard ML of New Jersey v110.75 [built: Sat Sep 29 12:51:13 2012]
- stdIn:21.18-21.35 Error: operator and operand don't agree [type mismatch]
operator domain: {1:'Y; 'Z}
operand: int list * int -> 'X
in expression:
(fn {1=1,...} => 1) split
stdIn:21.8-21.56 Error: operator and operand don't agree [type mismatch]
operator domain: {2:'Y; 'Z}
operand: int list * int -> 'X
in expression:
(fn {2=2,...} => 2) split
stdIn:22.8-22.56 Error: operator and operand don't agree [type mismatch]
operator domain: {1:'Y; 'Z}
operand: int list * int -> 'X
in expression:
(fn {1=1,...} => 1) split
stdIn:22.37-22.54 Error: operator and operand don't agree [type mismatch]
operator domain: {2:'Y; 'Z}
operand: int list * int -> 'X
in expression:
(fn {2=2,...} => 2) split
stdIn:24.4-24.35 Error: operator and operand don't agree [type mismatch]
operator domain: {1:'Y; 'Z}
operand: int list * int -> int list * _ list
in expression:
(fn {1=1,...} => 1) split
stdIn:24.49-24.80 Error: operator and operand don't agree [type mismatch]
operator domain: {2:'Y; 'Z}
operand: int list * int -> int list * _ list
in expression:
(fn {2=2,...} => 2) split
-
我将给定列表的头部设置为枢轴,并将列表拆分为 int 列表对并再次合并。
我认为不存在类型匹配问题,但我不知道为什么它不起作用。 请给我一些帮助:(
【问题讨论】: