module L01A where
import Test.QuickCheck
{-
(this is a comment block)
Lecture Week 1, part A
David Sands, 2012
-}
-- This is a single line comment
-- Plan:
-- Exchange rate calculation
-- concepts: simple data, types, testing, def by cases
-- type inference vs checking
-- exchangeRate
exchangeRate :: Double
exchangeRate = 8.68
-- toEU,toSEK
toEU s = s / exchangeRate
toSEK e = e * exchangeRate
prop_exchange s = toSEK (toEU s) ~== s
m ~== n = abs (m - n) < epsilon
where epsilon = 10e-14
-- abs m = if m < 0 then -m else m
abs' x | x < 0 = -x
| otherwise = x
-----------------------------------------------------------
-- Definition by recursion
-- power
power :: Integer -> Integer -> Integer
power n 0 = 1
power n k | k > 0 = n * power n (k - 1)
| otherwise = error "power on negative exponent"
prop_power n k = let k' = abs k in
power n k' == n^k'
-- More Types - tuples, lists, list comprehensions
-- Lists
example :: Double -> (Bool,Double)
example n = (n < 0, abs n)
-- second (x,y) = y
-- Functions over lists defined by pattern matching
snacks :: String
snacks = "Spam"
dinner = [snacks, "Fish", "Chips", snacks, snacks, "Pudding"]
summary :: [String] -> String
summary [] = "Nothing"
summary [x] = "Just " ++ x
summary [x,y] = x ++ " then " ++ y
summary (x:xs) = x
++ " then some other stuff and then finally "
++ last xs
-- len :: [t] -> Int
len [] = 0
len (x:xs) = 1 + len xs
-- len' xs = sum [ 1 | x <- xs]
doubles xs = [ 2 * x | x <- xs]
last' [] = error "last of empty list"
last' [x] = x
last' (x:xs) = last' xs
-- Functions over lists defined by recursion
-- length, last
-- List comprehensions
ex = [ 2 * x | x <- [1..20], odd x]
doubles xs = [2 * x | x <- xs]