Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Name: Zhang Xiaomenghan
- -- Exercise 2.1, 2.2, 2.3
- -- Exercise 2.1
- -- sumf (sumf square) [[1,2],[3,4]]
- -- sumf square [1,2] + sumf (sumf square) [[3,4]]
- -- square 1 + sumf square [2] + sumf (sumf square) [[3,4]]
- -- 1 + sumf square [2] + sumf (sumf square) [[3,4]]
- -- 1 + square 2 + sumf square [] + sumf (sumf square) [[3,4]]
- -- 1 + 4 + sumf square [] + sumf (sumf square) [[3,4]]
- -- 1 + 4 + 0 + sumf (sumf square) [[3,4]]
- -- 1 + 4 + 0 + sumf square [3,4] + sumf (sumf square) []
- -- 1 + 4 + 0 + square 3 + sumf square [4] + sumf (sumf square) []
- -- 1 + 4 + 0 + 9 + sumf square [4] + sumf (sumf square) []
- -- 1 + 4 + 0 + 9 + square 4 + sumf square [] + sumf (sumf square) []
- -- 1 + 4 + 0 + 9 + 16 + sumf square [] + sumf (sumf square) []
- -- 1 + 4 + 0 + 9 + 16 + 0 + sumf (sumf square) []
- -- 1 + 4 + 0 + 9 + 16 + 0 + 0
- -- 30
- -- Exercise 2.2
- module Product where
- import Prelude hiding (product)
- square :: Num n => n -> n
- square x = x ^ 2
- -- Implementation of product function
- product :: Num a => [a] -> a
- product [] = 1
- product (x:xs) = x * product xs
- -- Determining the product of the squares of the first numbers 1 through 10.
- answer :: Num a => a
- answer = product $ map square [1,2,3,4,5,6,7,8,9,10]
- -- END OF CODE
- -- GHCI run for answer
- -- *Product> answer
- -- 13168189440000
- -- SAMPLE GHCI RUNS
- -- *Product> product [12,4,5]
- -- 240
- -- *Product> product [1,1,1,5]
- -- 5
- -- Exercise 2.3
- module Results where
- divisibleBy :: Integer -> Integer -> Bool
- divisibleBy a b
- | b > a = divisibleBy a (b - a)
- | b == a = True
- | otherwise = False
- allp :: [a -> Bool] -> a -> Bool
- allp [] a = True
- allp (x:xs) a
- | null xs = x a
- | x a = allp xs a
- | otherwise = False
- filterAll :: [a -> Bool] -> [a] -> [a]
- filterAll = filter . allp
- result :: Integer
- result = sum
- . take 100
- . filter (divisibleBy 2)
- . filter (divisibleBy 3)
- . filter (not . divisibleBy 4)
- . filter (not . divisibleBy 9)
- $ [0..]
- result2 :: Integer
- result2 = sum
- . take 100
- . filter (allp [ divisibleBy 2
- , divisibleBy 3
- , not . divisibleBy 4
- , not . divisibleBy 9
- ])
- $ [0..]
- result3 :: Integer
- result3 = sum
- . take 100
- . filterAll [ divisibleBy 2
- , divisibleBy 3
- , not . divisibleBy 4
- , not . divisibleBy 9
- ]
- $ [0..]
- -- END OF CODE
- -- SAMPLE GHCI RUNS
- -- *Results> result
- -- 90000
- -- *Results> result2
- -- 90000
- -- *Results> result3
- -- 90000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement