Skip to content

Instantly share code, notes, and snippets.

@JustinSDK
Created December 22, 2014 07:43
Show Gist options
  • Save JustinSDK/28ca5d1fcdcc7a5fa704 to your computer and use it in GitHub Desktop.
Save JustinSDK/28ca5d1fcdcc7a5fa704 to your computer and use it in GitHub Desktop.
〈Haskell Tutorial(15)Typeclass 定義、實作與衍生〉的答案
instance Comp Integer where
comp x y = x - y
instance (Comp a) => Comp (Maybe a) where
comp (Just x) (Just y) = comp x y
comp (Just _) Nothing = 1
comp Nothing (Just _) = -1
comp Nothing Nothing = 0
quicksort :: Comp a => [a] -> [a]
quicksort [] = []
quicksort (x:xs) = quicksort small ++ (x : quicksort large)
where small = [y | y <- xs, comp x y >= 0]
large = [y | y <- xs, comp x y < 0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment