Advertisement
Guest User

Untitled

a guest
Oct 7th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Matthew Larson Exercise 2.2 10/7/2019
  2.  
  3.     import Prelude hiding (sum)
  4.  
  5.     sum :: Num n => [n] -> n
  6.     sum [] = 0
  7.     sum (x:xs) = x + sum xs
  8.  
  9.     -- | Square a number
  10.  
  11.     square :: Num n => n -> n
  12.     square x = x^2
  13.  
  14.     sumProducts :: Num n => [n] -> n
  15.  
  16.     sumProducts = product . map square
  17.  
  18.     divisibleBy :: Integral n => n -> n -> Bool
  19.     divisibleBy d n = d `mod` n == 0
  20.  
  21.  
  22.     result = sum
  23.            . take 100
  24.            . filter (divisibleBy 2)
  25.            . filter (divisibleBy 3)
  26.            . filter (not . divisibleBy 4)
  27.            . filter (not . divisibleBy 9)
  28.            $ [0..]
  29.  
  30.     allp :: [a-> Bool] -> Bool -> Bool
  31.     allp [] x = False
  32.     allp (p:ps) x
  33.       | p x = allp ps x
  34.       | otherwise = False
  35.  
  36.     filterAll :: (a-> [b]) -> (c -> Bool)-> [a]
  37.     filterAll filter . (allp (c:cs)) = c: filterAll filter allp cs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement