sales i = iterate (\i -> 1 + 7 * i `mod` 1001) 3 !! (i - 1) -- details of this definition are not important! ----------------------------------------- -- Introduction to Functional Programming -- 3A List continued: list comprehensions -- David Sands -- Weekly sales from a shop, week numbers start at zero -- sales i is the sales for week i. -- Assume week (Int) is positive (> 0). -- Result is always non-negative type Week = Int type Sales = Integer sales :: Week -> Sales -- definition not important -- (1) total sales for the first n weeks? total :: Week -> Sales total n = sum (allSales n) -- (2) highest sale in the first n weeks? highest :: Week -> Sales highest n = maximum (allSales n) allSales :: Week -> [Sales] allSales n = [sales w | w <- [1..n]] -- (3) number of weeks with sales less than 100 -- in the first n weeks. numBadWeeks :: Week -> Int numBadWeeks n = length [ s | s <- allSales n, s < 100] -- Exercise: solve these with recursion, without using lists -- (4) Compute a table of week number and sales up to week n. table :: Week -> [(Week,Sales)] -- table n = [(w,sales w) | w <- [1..n]] table n = zip [1..] (allSales n) -- broken! [(w,s) | s <- allSales n, w <- [1..n]] -- (4) Give the week number of the week with the best sales in the first n weeks. -- (5) Write a quickcheck property for (4) -- (6) Hard Exercise -- Compute the longest number of consecutive weeks with -- sales less than 100 (in the weeks up to and including week n). -- (7) Make a table for all the sales upto week n