И така, написах тази функция за бързо сортиране в SML, за да използвам сгъването на функцията от висок ред, но тя се задържа в безкраен цикъл и не мога да определя грешната логика, която го причинява. Някакви предложения къде да търся?
(* takes in a list of numbers and an arbitrary binary relation function f *)
fun quicksort nil f = []
| quicksort [x] f = [x]
| quicksort list f =
let
(* simply choose pivot as first item in the list *)
val pivot = hd list
(* lists iterated by folding for numbers pertaining to the relation f
or its converse *)
fun test a = List.foldr (fn (x,y) => if f (pivot, x) then x::y else y) [] a
fun testC a = List.foldr (fn (x,y) => if f (pivot, x) then y else x::y) [] a
in
(* my notion is the function is looping here, since the functions test
and testC work fine on their own *)
quicksort (test list) op f @ [pivot] @ quicksort (testC list) op f
end;
Благодаря за всякакви предложения.