Created
June 12, 2018 09:48
-
-
Save jc99kuo/d5ef2450cb38090ac2efe14116bd37c2 to your computer and use it in GitHub Desktop.
Prerequisites in FLOLAC 2018
This file contains hidden or 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
-- Answers for Prerequisites in FLOLAC 2018 | |
-- 1) myFst | |
myFst :: (a, b) -> a | |
myFst (x, _) = x | |
-- 2) myOdd | |
myOdd :: Int -> Bool | |
myOdd n = (mod n 2) /= 0 | |
-- 3) Answers: | |
{- | |
(a) Ord is a class of type where any two value in the type can be compared and get result | |
as one of {LT, EQ, GT}, i.e. the type is totally ordered. | |
qs :: Ord a => [a] -> [a] | |
Mean for all type a which is of class Ord, i.e. equipped with a totally ordered relation | |
for all of its elements. Function qs can accept a list of type a and produce another list | |
of type a. | |
(b) The type of (++) is | |
(++) :: [a] -> [a] -> [a] | |
-- i.e. take two lists of same type a, produce a combined list of same type a. | |
But due to type declaration of qs and type inference by Haskell, the type become | |
(++) :: Ord a => [a] -> [a] -> [a] | |
The functionality of (++) is to append two list and maintain orders of all elements respectively. | |
(c) Elements of ys are those elements in xs which are less or equal (<=) to x. | |
Elements of zs are those elements in xs which are greater than x. | |
(d) The function qs is an implemetation of quick sort. | |
Where qs pick one element (the head of the list) then split the tail into two lists where one contains | |
less-or-equal elements and the other contains strictly greater ones, the sort two listd recursively. | |
(e) Please see following code | |
-} | |
qs :: Ord a => [a] -> [a] | |
qs list = case list 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.