Advertisement
Guest User

Untitled

a guest
Oct 5th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Name: Zhang Xiaomenghan
  2. -- Exercise 2.1, 2.2, 2.3
  3.  
  4. -- Exercise 2.1
  5. -- sumf (sumf square) [[1,2],[3,4]]
  6. -- sumf square [1,2] + sumf (sumf square) [[3,4]]
  7. -- square 1 + sumf square [2] + sumf (sumf square) [[3,4]]
  8. -- 1 + sumf square [2] + sumf (sumf square) [[3,4]]
  9. -- 1 + square 2 + sumf square [] + sumf (sumf square) [[3,4]]
  10. -- 1 + 4 + sumf square [] + sumf (sumf square) [[3,4]]
  11. -- 1 + 4 + 0 + sumf (sumf square) [[3,4]]
  12. -- 1 + 4 + 0 + sumf square [3,4] + sumf (sumf square) []
  13. -- 1 + 4 + 0 + square 3 + sumf square [4] + sumf (sumf square) []
  14. -- 1 + 4 + 0 + 9 + sumf square [4] + sumf (sumf square) []
  15. -- 1 + 4 + 0 + 9 + square 4 + sumf square [] + sumf (sumf square) []
  16. -- 1 + 4 + 0 + 9 + 16 + sumf square [] + sumf (sumf square) []
  17. -- 1 + 4 + 0 + 9 + 16 + 0 + sumf (sumf square) []
  18. -- 1 + 4 + 0 + 9 + 16 + 0 + 0
  19. -- 30
  20.  
  21. -- Exercise 2.2
  22. module Product where
  23. import Prelude hiding (product)
  24.  
  25. square :: Num n => n -> n
  26. square x = x ^ 2
  27.  
  28. -- Implementation of product function
  29. product :: Num a => [a] -> a
  30. product [] = 1
  31. product (x:xs) = x * product xs
  32.  
  33. -- Determining the product of the squares of the first numbers 1 through 10.
  34. answer :: Num a => a
  35. answer = product $ map square [1,2,3,4,5,6,7,8,9,10]
  36.  
  37. -- END OF CODE
  38.  
  39. -- GHCI run for answer
  40. -- *Product> answer
  41. -- 13168189440000
  42.  
  43. -- SAMPLE GHCI RUNS
  44. -- *Product> product [12,4,5]
  45. -- 240
  46. -- *Product> product [1,1,1,5]
  47. -- 5
  48.  
  49. -- Exercise 2.3
  50.  
  51. module Results where
  52.  
  53. divisibleBy :: Integer -> Integer -> Bool
  54. divisibleBy a b
  55.     | b > a = divisibleBy a (b - a)
  56.     | b == a = True
  57.     | otherwise = False
  58.  
  59. allp :: [a -> Bool] -> a -> Bool
  60. allp [] a = True
  61. allp (x:xs) a
  62.     | null xs = x a
  63.     | x a = allp xs a
  64.     | otherwise = False
  65.  
  66. filterAll :: [a -> Bool] -> [a] -> [a]
  67. filterAll = filter . allp
  68.  
  69. result :: Integer
  70. result = sum
  71.     . take 100
  72.     . filter (divisibleBy 2)
  73.     . filter (divisibleBy 3)
  74.     . filter (not . divisibleBy 4)
  75.     . filter (not . divisibleBy 9)
  76.     $ [0..]
  77.  
  78. result2 :: Integer
  79. result2 = sum
  80.     . take 100
  81.     . filter (allp [ divisibleBy 2
  82.                     , divisibleBy 3
  83.                     , not . divisibleBy 4
  84.                     , not . divisibleBy 9
  85.                     ])
  86.     $ [0..]
  87.  
  88. result3 :: Integer
  89. result3 = sum
  90.     . take 100
  91.     . filterAll [ divisibleBy 2
  92.                 , divisibleBy 3
  93.                 , not . divisibleBy 4
  94.                 , not . divisibleBy 9
  95.                 ]
  96.     $ [0..]
  97.  
  98. -- END OF CODE
  99.  
  100. -- SAMPLE GHCI RUNS
  101. -- *Results> result
  102. -- 90000
  103. -- *Results> result2
  104. -- 90000
  105. -- *Results> result3
  106. -- 90000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement