import Test.QuickCheck -- Lecture Week 1, part A -- David Sands, 2017 -- Plan: FPFTW -- Price calculation -- concepts: simple data, types, testing, def by cases -- The price of prawns: 275kr/Kg unitPrice = 275.0 -- kr/Kg discountPrice = unitPrice - discount discount = 75.0 -- kr/Kg -- functions price kg = unitPrice * kg discountedPrice kg = discountPrice * kg threshold = 50 {- Suppose price gets cheaper if we buy more: 275/Kg up to 50 kg, and a discount of 75 kr/Kg for everything above 50kg -} price2 kg | kg <= threshold = price kg | kg > threshold = price threshold + discountedPrice (kg - threshold) prop_price2 kg delta = price2 (abs kg) <= price2 (abs kg + abs delta) -- Example of def by cases abs' n | n >= 0 = n | n < 0 = -n -- Definition by cases (guards) ----------------------------------------------------------- -- Definition by recursion -- 5! = 5 * 4 * 3 * 2 * 1 fac n | n == 0 = 1 | n > 0 = n * fac (n-1) -- primitive recursion {- fac 3 3 * fac 2 3 * 2 * fac 1 3 * 2 * 1 * fac 0 3 * 2 * 1 * 1 6 -} -- power2 k computes 2 to the power k, -- for natural numbers k. -- power2 3 = 2 * 2 * 2 power2 n | n == 0 = 1 | n > 0 = 2 * power2 (n-1) -- Other types: Lists and comprehensions -- Literal lists -- Strings -- Simple generators -- List comprehensions -- Further info: online materials ----------- upto n = [1..n] fac' n = product [1..n] -- List comprehensions (on Friday)