fun merge_sort (_, nil) = nil
| merge_sort (_, [a]) = [a]
| merge_sort (f, L) =
let
fun halve nil = (nil,nil)
| halve [a] = ([a], nil)
| halve (a :: b :: rest) =
let
val (x , y) = halve rest
in
(a :: x, b :: y)
end
fun merge (f, nil, x) = x
| merge (f, x, nil) = x
| merge (f, a::b, x::y) =
if f(a, b) then a :: merge (f, b, x::y)
else x :: merge (f, a::b, y);
val (x, y) = halve L
in
merge(f, merge_sort(f, x), merge_sort(f,y))
end;
merge_sort (op <) [2,1,3,2,4,3,45];
This is a SML function that I have been working on. It is meant to take a function call as shown in the bottom and merge sort. Must be one function. I am struggling understanding the pattern matching and how to fully make this function work.
I get this error code when I compile and run it.
$sml < main.sml
Standard ML of New Jersey v110.78 [built: Thu Aug 31 03:45:42 2017]
- val merge_sort = fn : ('a * 'a list -> bool) * 'a list -> 'a list
stdIn:23.1-23.35 Error: operator and operand don't agree [tycon mismatch]
operator domain: ('Z * 'Z list -> bool) * 'Z list
operand: [< ty] * [< ty] -> bool
in expression:
merge_sort <
-
I don't entirely know what I am doing wrong