Skip to content

Instantly share code, notes, and snippets.

@kevin47
Created May 31, 2018 11:28
Show Gist options
  • Save kevin47/fd54475175d06efc12a7f18dcfd590a5 to your computer and use it in GitHub Desktop.
Save kevin47/fd54475175d06efc12a7f18dcfd590a5 to your computer and use it in GitHub Desktop.
myFst :: (a, b) -> a
myFst x = fst x
myOdd :: Int -> Bool
myOdd x = odd x
-- (a) Ord is a typeclass. It means that the type supports the functions of the Ord typeclass (e.g. compare, <, > etc.).
-- The type of qs mean that "a" belongs to typeclass "Ord". The function qs take a list of type "a" and return a list of type "a"
-- (b) The type of ++ is (++) :: [a] -> [a] -> [a]. It concatenates 2 lists.
-- (c) ys are the elements that are less or equal than x(the pivot of quicksort). zs are the elements that are greater than x.
-- (d) The function is quicksort.
-- (e)
qs :: Ord a => [a] -> [a]
qs a = case a of [] -> []
(x:xs) ->
let ys = [ y | y <- xs, y <= x ]
zs = [ z | z <- xs, x < z ]
in qs ys ++ [x] ++ qs zs
@L-TChen
Copy link

L-TChen commented Jul 3, 2018

Well done.

type supports the functions of the Ord typeclass

To be precise, every instance of Ord has a total order given by compare.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment