Search code examples
smlsmlnj

Quicksort in SML operator and operand don't agree error


I am trying to write a quicksort function of type

'a list * ('a * 'a -> bool) -> 'a list

but for some reason I am getting:

'a list -> ('a * 'a -> bool) -> 'a list

Here is my code for the function:

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

When I call the function I get this error:

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 <)

I realize that I must be passing it in wrong, but I just can't see my mistake. So, my question is what is going on here in which I get this error while trying to pass in my list and operator?


Solution

  • You could simple change:

    fun quicksort xs f = let 
    

    to:

    fun quicksort (xs,f) = let
    

    since you want quicksort to have as parameters a tuple (xs,f).