functional version of quicksort