O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open src/Main.hs and write the qsort implementation. The qsort involves the following:
    • Choosing an element of the list to be sorted
    • Using the chosen element as a pivot, divide the input list into two parts:
      • Subset of the list smaller than the pivot element
      • Subset of the list greater than or equal to the pivot element
    • Recursively sorting two parts in a similar way
  2.  In Haskell, we can implement a method like quick sort quite easily. Ord a in the qsort declaration signifies that the elements of the list [a] can be compared for inequality, and it is possible to use the operators <, >, <=, and >=:
        qsort :: Ord a => [a] -> [a]        qsort [] = []        qsort (x:xs) = qsort ys ++ [x] ++ qsort zs        where          ys = filter (\y -> y < x) xs zs ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required