module Exercise_2_Sol where import Data.List import Test.QuickCheck {-G2.1-} allSums :: [Integer] -> [Integer] -> [Integer] allSums xs ys = [x + y | x <- xs, y <- ys] evens :: [Integer] -> [Integer] evens xs = [x | x <- xs, x `mod` 2 == 0] nLists :: [Integer] -> [[Integer]] nLists xs = [[1..x] | x <- xs] allEvenSumLists :: [Integer] -> [Integer] -> [[Integer]] allEvenSumLists xs ys = [[1..x+y] | x <- xs, y <- ys, (x + y) `mod` 2 == 0] prop_allEvenSumLists xs ys = allEvenSumLists xs ys == nLists (evens (allSums xs ys)) {-G2.2-} union :: [Integer] -> [Integer] -> [Integer] union xs ys = xs ++ ys intersection :: [Integer] -> [Integer] -> [Integer] intersection xs ys = [x | x <- xs, y <- ys, x == y] diff :: [Integer] -> [Integer] -> [Integer] diff xs ys = [x | x <- xs, not (x `elem` ys)] elem' :: Integer -> [Integer] -> Bool elem' x xs = not (null [y | y <- xs, y == x]) prop_inter_1 x xs ys = x `elem` xs ==> x `elem` ys ==> x `elem` intersection xs ys prop_inter_2 x xs ys = x `elem` intersection xs ys ==> x `elem` xs && x `elem` ys prop_inter_3 x xs ys = (x `elem` xs && x `elem` ys) == (x `elem` intersection xs ys) {-G2.3-} eqFrac :: (Integer,Integer) -> (Integer,Integer) -> Bool eqFrac (a,b) (c,d) = a*d == c * b prop_eqFrac_scale a b n = eqFrac (a, b) (n * a, n * b) prop_eqFrac_refl a b = eqFrac (a, b) (a, b) {-G2.4-} pow2_slow :: Integer -> Integer pow2_slow 0 = 1 pow2_slow n | n > 0 = 2 * pow2_slow (n - 1) pow2 :: Integer -> Integer pow2 0 = 1 pow2 n | n < 0 = undefined | n `mod` 2 == 0 = k * k | otherwise = 2 * pow2 (n - 1) where k = pow2 (n `div` 2) {-H2.1-} index :: [Char] -> Char -> Int index xs c = index' 0 where index' n | n < length xs = if xs !! n == c then n else index' (n + 1) | otherwise = n {-H2.2-} hasDraws :: [(String, Integer)] -> Bool hasDraws namePointList = nub pointList /= pointList where pointList = [points | (_, points) <- namePointList] {-H2.3-} intLists _ 0 = [[]] intLists (m,n) k | k < 0 = [] | otherwise = [ i : is | i <- [m..n], is <- intLists (m,n) (k - 1)] {-H2.4-} decodeIntPair :: Integer -> (Integer, Integer) decodeIntPair 0 = (0, 0) decodeIntPair n = (2 * a + d, 2 * b + e) where (a, b) = decodeIntPair (n `div` 4) d = n `mod` 2 e = (n `div` 2) `mod` 2 decodeIntPairs :: [Integer] -> [(Integer, Integer)] decodeIntPairs ns = [decodeIntPair n | n <- ns, n >= 0]