Skip to content

Instantly share code, notes, and snippets.

@kazu-yamamoto
Created January 28, 2013 01:41
Show Gist options
  • Save kazu-yamamoto/4652121 to your computer and use it in GitHub Desktop.
Save kazu-yamamoto/4652121 to your computer and use it in GitHub Desktop.
QuickSort with differential list
module QuickSortWithDL where
import Data.Monoid
data DL a = DL ([a] -> [a])
instance Monoid (DL a) where
mempty = DL ([] ++)
DL f `mappend` DL g = DL (f . g)
singleton :: a -> DL a
singleton x = DL ([x] ++)
qsort :: Ord a => [a] -> [a]
qsort xs = builder []
where
DL builder = qsort' xs
qsort' :: Ord a => [a] -> DL a
qsort' [] = mempty
qsort' (x:xs) = le <> singleton x <> gt
where
le = qsort' [y|y<-xs,y<x]
gt = qsort' [y|y<-xs,y>x]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment