import Data.List {- Exercise 8.1 -} -- powerlistFR :: powerlistFR xs = undefined -- powerlistFL :: powerlistFL = undefined {- Exercise 8.2 -} insertS :: Ord a => a -> [a] -> [a] insertS x [] = [x] insertS x (y:ys) | x < y = x:y:ys | otherwise = y:insertS x ys {- 8.2.1 -} iSort [] = undefined iSort (x:xs) = undefined {- 8.2.2. -} iSortFold xs = undefined {- Exercise 8.3 -} {- 8.3.1 -} all_fold :: (a -> Bool) -> [a] -> Bool all_fold p = undefined {- 8.3.2 -} dig2int :: [Integer] -> Integer dig2int [] = 0 dig2int (x:xs) = x + 10 * dig2int xs dig2int_fold :: [Integer] -> Integer dig2int_fold = undefined {- Exercise 8.4 -} {- 8.4.1 -} number :: [a] -> [(Int,a)] number xs = undefined {- 8.4.2 -} evenProdSum :: [Int] -> Int evenProdSum xs = undefined {- TESTS -} test_list f g ss st = all (test_fun f g) ss where test_fun f g s = f s == g s || error ("function " ++ st ++ " on input " ++ show s ++ " delivered " ++ show (g s) ++ ", but expected was " ++ show (f s)) {- 8.1 -} testPowerlist f = sort (f xs) == sort (subsequences xs) where xs = [1..5] test_PowerlistFL = testPowerlist powerlistFL test_PowerlistFR = testPowerlist powerlistFR {- 8.2 -} test_iSort = test_list sort iSort ss "iSort" where ss = [[2,3,4], [2, 9, 7],[6,6,6,6],[8,2,5,1,3,5],[2,1,8,1,6,0]] test_iSortFold = test_list sort iSortFold ss "iSortFold" where ss = [[2,3,4], [2, 9, 7],[6,6,6,6],[8,2,5,1,3,5],[2,1,8,1,6,0]] {- 8.3 -} test_all_fold = test_list (all (<='A')) (all_fold (<='A'))ss "test_all_fold" where ss = [['a','B','d'], ['Z', 'd', 'f'],['z','a','b','o'],['A','P','K','L', 'M','E'],['w','q','g','n','e','t']] test_dig2int_fold = test_list dig2int dig2int_fold ss "dig2int_fold" where ss = [[2,3,4], [2, 9, 7],[6,6,6,6],[8,2,5,1,3,5],[2,1,8,1,6,0]] {- 8.4 -} test_number = test_list number number' tests "number" where number' xs = [ (i, xs !! i) | i <- [0 .. length xs - 1]] tests = ["hello", "", "world!!!"] test_evenProdSum = test_list evenProdSum (evenProdSum' 2) tests "evenProdSum" where tests = [[], [5], [8493,49,1,3,2], replicate 15 6, replicate 16 6] evenProdSum' _ [] = 0 evenProdSum' _ [_] = 0 evenProdSum' n (_ : x : xs) = n * x + evenProdSum' (n+2) xs testAll = test_PowerlistFL && test_PowerlistFR && test_iSort && test_iSortFold && test_all_fold && test_dig2int_fold && test_number && test_evenProdSum