Advertisement
Guest User

Untitled

a guest
Oct 7th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Gabe Schoenbach
  2.  
  3. -- | A module for doing arithmetic with lists.
  4.  
  5. module Homework where
  6.   import Prelude hiding (product)
  7.  
  8.   -- Exercise 2.1
  9.  
  10.   sumf (sumf square) [[1,2],[3,4]]
  11.   sumf square [1,2] + sumf sumf square [3,4]
  12.   square 1 + sumf square 2 + sumf square 3 + sumf sumf square 4
  13.   1 + square 2 + sumf square [] + square 3 + sumf square [] + sumf square 4 + sumf sumf square []
  14.   1 + 4 + 0 + 9 + 0 + square 4 + sumf square [] + sumf square [] + sumf sumf square []
  15.   14 + 16 + 0 + 0 + 0
  16.   30
  17.  
  18.   -- Exercise 2.2
  19.  
  20.   -- | Compute the product of the numbers in a list
  21.   product :: Num n => [n] -> n
  22.   product [] = 1
  23.   product (c:cs) = c * product cs
  24.  
  25.   -- | Square a number
  26.   square :: Num n => n -> n
  27.   square x = x^2
  28.  
  29.   -- | Determine the product of squares of a list
  30.   productSquares :: [Integer] -> Integer
  31.   productSquares = product . map square
  32.   -- productSquares [1..10] = 13168189440000
  33.  
  34.   -- Exercise 2.3
  35.  
  36.   -- | Determine if a number n is divisible by d
  37.   divisibleBy :: Integral n => n -> n -> Bool
  38.   divisibleBy d n
  39.     | n `mod` d == 0 = True
  40.     | otherwise = False
  41.  
  42.   -- | Given a list of predicates ps and a value x, output True iff p x is True for all p in ps
  43.   allp :: [x -> Bool] -> x -> Bool
  44.   allp [] x = True
  45.   allp (p:ps) x
  46.     | p x = allp ps x
  47.     | otherwise = False
  48.  
  49.   -- | Given a list of predicates ps and a list of values, filter the list down to only the
  50.   -- values that satisfy every predicate
  51.   filterAll :: [x -> Bool] -> [x] -> [x]
  52.   filterAll = filter . allp
  53.  
  54.   -- Compare the results of using different methods to sum numbers.
  55.   result1 :: Integer
  56.   result1 = sum
  57.           . take 100
  58.           . filter (divisibleBy 2)
  59.           . filter (divisibleBy 3)
  60.           . filter (not . divisibleBy 4)
  61.           . filter (not . divisibleBy 9)
  62.           $ [0..]
  63.   -- result1 = 90000
  64.  
  65.   result2 :: Integer
  66.   result2 = sum
  67.           . take 100
  68.           . filter (allp [ divisibleBy 2
  69.                          , divisibleBy 3
  70.                          , not . divisibleBy 4
  71.                          , not . divisibleBy 9
  72.                           ])
  73.           $ [0..]
  74.   -- result2 = 90000
  75.  
  76.   result3 :: Integer
  77.   result3 = sum
  78.           . take 100
  79.           . filterAll [ divisibleBy 2
  80.                       , divisibleBy 3
  81.                       , not . divisibleBy 4
  82.                       , not . divisibleBy 9
  83.                       ]
  84.           $ [0..]
  85.   -- result3 = 90000
  86.  
  87.   -- Exercise 2.4 (this is incorrect and unfinished)
  88.  
  89.   -- | Given a list of predicates and a value x, return True iff p x holds for all x, but define it without recursion.
  90.  
  91.   -- | Given a predicate and a value, return its truth value.
  92.   eval :: [x -> Bool] -> x -> Bool
  93.   eval p x
  94.     | p x = True
  95.     | otherwise = False
  96.  
  97.   allP :: [x -> Bool] -> x -> [Bool]
  98.   allP (p:ps) x
  99.     | eval p x : ps
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement