Guest User

Untitled

a guest
Jun 14th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. -- Answers for Prerequisites in FLOLAC 2018
  2.  
  3. -- 1) myFst
  4.  
  5. myFst :: (a, b) -> a
  6. myFst (x, _) = x
  7.  
  8. -- 2) myOdd
  9.  
  10. myOdd :: Int -> Bool
  11. myOdd n = (mod n 2) /= 0
  12.  
  13. -- 3) Answers:
  14. {-
  15. (a) Ord is a class of type where any two value in the type can be compared and get result
  16. as one of {LT, EQ, GT}, i.e. the type is totally ordered.
  17.  
  18. qs :: Ord a => [a] -> [a]
  19. Mean for all type a which is of class Ord, i.e. equipped with a totally ordered relation
  20. for all of its elements. Function qs can accept a list of type a and produce another list
  21. of type a.
  22.  
  23. (b) The type of (++) is
  24. (++) :: [a] -> [a] -> [a]
  25. -- i.e. take two lists of same type a, produce a combined list of same type a.
  26. But due to type declaration of qs and type inference by Haskell, the type become
  27. (++) :: Ord a => [a] -> [a] -> [a]
  28.  
  29. The functionality of (++) is to append two list and maintain orders of all elements respectively.
  30.  
  31. (c) Elements of ys are those elements in xs which are less or equal (<=) to x.
  32. Elements of zs are those elements in xs which are greater than x.
  33.  
  34. (d) The function qs is an implemetation of quick sort.
  35. Where qs pick one element (the head of the list) then split the tail into two lists where one contains
  36. less-or-equal elements and the other contains strictly greater ones, the sort two listd recursively.
  37.  
  38. (e) Please see following code
  39. -}
  40.  
  41. qs :: Ord a => [a] -> [a]
  42. qs list = case list of
  43. [] -> []
  44. (x:xs) -> let
  45. ys = [ y | y <- xs, y <= x]
  46. zs = [ z | z <- xs, x < z]
  47. in
  48. qs ys ++ [x] ++ qs zs
Add Comment
Please, Sign In to add comment