(* Paulson2, p.110 *) fun quick [] = [] | quick [x] = [x] | quick (a::bs) = (* the head "a" is the pivot *) let fun partition (left, right, []): real list = (quick left) @ (a :: quick right) | partition (left, right, x::xs) = if x <= a then partition(x::left, right, xs) else partition (left, x::right, xs) in partition([],[],bs) end; (* Simpler Version *) fun partition(x, []) = ([], []) | partition(x, y::ys) = let val (ls, ms) = partition(x, ys) in if y < x then (y::ls, ms) else (ls, y::ms) end; fun quick [] = [] | quick [x] = [x] | quick (x::xs) = let val (less, more) = partition(x, xs) in quick(less) @ (x::quick(more)) end;