SML 运算符和操作数中的快速排序不一致错误
Quicksort in SML operator and operand don't agree error
我正在尝试编写
类型的快速排序函数
'a list * ('a * 'a -> bool) -> 'a list
但出于某种原因我得到:
'a list -> ('a * 'a -> bool) -> 'a list
这是我的函数代码:
fun quicksort xs f = let
fun qs [] = []
| qs [x] = [x]
| qs (p::xs) = let
val (less, more) = List.partition (fn x => f (x, p)) xs
in
qs less @ p :: qs more
end
in
qs xs
end
当我调用该函数时出现此错误:
stdIn:73.1-73.18 Error: operator and operand don't agree [tycon mismatch]
operator domain: 'Z list
operand: int list * (int * int -> bool)
in expression:
quicksort (L, op <)
我意识到我一定是传错了,但我就是看不到我的错误。所以,我的问题是这里发生了什么,我在尝试传递我的列表和运算符时收到此错误?
您可以简单地更改:
fun quicksort xs f = let
至:
fun quicksort (xs,f) = let
因为您希望快速排序将元组 (xs,f) 作为参数。
我正在尝试编写
类型的快速排序函数'a list * ('a * 'a -> bool) -> 'a list
但出于某种原因我得到:
'a list -> ('a * 'a -> bool) -> 'a list
这是我的函数代码:
fun quicksort xs f = let
fun qs [] = []
| qs [x] = [x]
| qs (p::xs) = let
val (less, more) = List.partition (fn x => f (x, p)) xs
in
qs less @ p :: qs more
end
in
qs xs
end
当我调用该函数时出现此错误:
stdIn:73.1-73.18 Error: operator and operand don't agree [tycon mismatch]
operator domain: 'Z list
operand: int list * (int * int -> bool)
in expression:
quicksort (L, op <)
我意识到我一定是传错了,但我就是看不到我的错误。所以,我的问题是这里发生了什么,我在尝试传递我的列表和运算符时收到此错误?
您可以简单地更改:
fun quicksort xs f = let
至:
fun quicksort (xs,f) = let
因为您希望快速排序将元组 (xs,f) 作为参数。