Skip to content

Instantly share code, notes, and snippets.

@sasdf
Created May 18, 2018 13:21
Show Gist options
  • Save sasdf/54bc8ee6e5a54883d0a0051c5d61be98 to your computer and use it in GitHub Desktop.
Save sasdf/54bc8ee6e5a54883d0a0051c5d61be98 to your computer and use it in GitHub Desktop.
FLOLAC '18 preparation answer
{- 1 -}
myFst :: (a, b) -> a
myFst x = fst x
{- 2 -}
myOdd :: Int -> Bool
myOdd x = mod x 2 == 1
{- 3 -}
-- (a) Ord is for types that have an ordering.
--
-- (b) (++) :: [a] -> [a] -> [a]
-- ++ concatenates two list.
--
-- (c) ys is a set of all elements smaller than x,
-- while zs is a set of all elements larger than x.
--
-- (d) qs is quicksort with first element as pivot.
--
-- (e)
qs' :: (Ord a) => [a] -> [a]
qs' xs =
case xs of
[] -> []
(x:xs) ->
let ys = [y | y <- xs, y <= x]
zs = [y | y <- xs, y > x]
in
qs' ys ++ [x] ++ qs' zs
@L-TChen
Copy link

L-TChen commented Jul 3, 2018

Well done.

(a) Ord is for types that have an ordering.

To be precise, every instance of Ord has a total order provided by compare:: a -> a -> Ordering.

... smaller than x, ...

should be "... smaller than or equal to x..."

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