-
-
Save kevin47/fd54475175d06efc12a7f18dcfd590a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well done.
To be precise, every instance of
Ord
has a total order given bycompare
.