I learned SML last week so I tried to make Quicksort code to understand it well. My code is:
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
and this is the error message:
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
-
I set the head of the given list as pivot, and split the list into int list pairs and merge them again.
I think there are no type-matching problem but I don't no why it doesn't works. Plz give some help for me :(