Download Week 3

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
COSC 4P41 – Functional Programming
Example Picture
type Picture = [[Char]]
horse :: Picture
horse =
[[’.’,’.’,’.’,’.’,’.’,’.’,’.’,’#’,’#’,’.’,’.’,’.’],
[’.’,’.’,’.’,’.’,’.’,’#’,’#’,’.’,’.’,’#’,’.’,’.’],
[’.’,’.’,’.’,’#’,’#’,’.’,’.’,’.’,’.’,’.’,’#’,’.’],
[’.’,’.’,’#’,’.’,’.’,’.’,’.’,’.’,’.’,’.’,’#’,’.’],
[’.’,’.’,’#’,’.’,’.’,’.’,’#’,’.’,’.’,’.’,’#’,’.’],
[’.’,’.’,’#’,’.’,’.’,’.’,’#’,’#’,’#’,’.’,’#’,’.’],
[’.’,’#’,’.’,’.’,’.’,’.’,’#’,’.’,’.’,’#’,’#’,’.’],
[’.’,’.’,’#’,’.’,’.’,’.’,’#’,’.’,’.’,’.’,’.’,’.’],
[’.’,’.’,’.’,’#’,’.’,’.’,’.’,’#’,’.’,’.’,’.’,’.’],
[’.’,’.’,’.’,’.’,’#’,’.’,’.’,’#’,’.’,’.’,’.’,’.’],
[’.’,’.’,’.’,’.’,’.’,’#’,’.’,’#’,’.’,’.’,’.’,’.’],
[’.’,’.’,’.’,’.’,’.’,’.’,’#’,’#’,’.’,’.’,’.’,’.’]]
© M. Winter
3.1
COSC 4P41 – Functional Programming
Example Picture (cont’d)
printPicture :: Picture -> IO ()
printPicture = putStr . concat . map (++"\n")
printPicture horse
.......##...
.....##..#..
...##.....#.
..#.......#.
..#...#...#.
..#...###.#.
.#....#..##.
..#...#.....
...#...#....
....#..#....
.....#.#....
......##....
© M. Winter
3.2
COSC 4P41 – Functional Programming
Example Picture (cont’d)
printPicture :: Picture -> IO ()
printPicture = putStr . concat . map (++"\n")
rotate90 :: Picture -> Picture
rotate90 pic = [ [ line !! i | line <- reverse pic ]
| i <- [0..length pic - 1] ]
scale :: Picture -> Int -> Picture
scale pic n
| n <= 0
= [[]]
| otherwise
= map (concat . map (replicate n)) pic
© M. Winter
3.3
COSC 4P41 – Functional Programming
Local definitions
There are two syntactic constructions for local definitions
• let … in …
• … where …
doubleSquare :: Int -> Int
doubleSquare x = let sqx = x*x in sqx + sqx
strange :: Int -> Int -> Int
strange x y =
let diff = x – y
sum = x + y
in diff^2 – diff + sum^2 - sum
strange x y = let diff = x – y; sum = x + y in …
© M. Winter
3.4
COSC 4P41 – Functional Programming
maxsq :: Int -> Int -> Int
maxsq x y
| sqx > sqy = sqx
| otherwise = sqy
where
sqx = sq x
sqy = sq y
sq :: Int -> Int
sq z = z*z
maxsq x y
| sqx > sqy = sqx
| otherwise = sqy
where
sqx = sq x
sqy = sq y
sq :: Int -> Int
sq x = x*x
© M. Winter
3.5
COSC 4P41 – Functional Programming
Example: Supermarket billing
List of bar codes: [1234,4719,3814,1112,1113,1234]
Produce a bill:
Haskell Stores
Dry Sherry, 1lt...........5.40
Fish Fingers..............1.21
Orange Jelly..............0.56
Hula Hoops (Giant)........1.33
Unknown Item..............0.00
Dry Sherry, 1lt...........5.40
Total....................13.90
© M. Winter
3.6
COSC 4P41 – Functional Programming
Example: Supermarket billing (cont’d)
type Name
= String
type Price
= Int
type BarCode = Int
type Database = [(BarCode,Name,Price)]
codeIndex = [ (4719,”Fish Fingers”,121),
(5643,”Nappies”,1010),
(3814,”Orange Jelly”,56),
(1111,”Hula Hoops”,21),
(1112,”Hula Hoops (Giant)”,133),
(1234,”Dry Sherry, 1lt”,540)]
© M. Winter
3.7
COSC 4P41 – Functional Programming
Example: Supermarket billing (cont’d)
type TillType = [BarCode]
type BillType = [(Name,Price)]
makeBill :: TillType -> BillType
formatBill :: BillType -> String
produceBill :: TillType -> String
lineLength :: Int
lineLength = 30
© M. Winter
3.8
COSC 4P41 – Functional Programming
Example: Supermarket billing (cont’d)
look :: Database -> BarCode -> (Name,Price)
look db b = if res == [] then ("Unknown Item",0) else head res
where res = [ (n,p) | (bn,n,p) <- db, b == bn ]
lookup :: BarCode -> (Name,Price)
lookup = look codeIndex
makeBill :: TillType -> BillType
makeBill = map lookup
formatPence :: Price -> String
formatPence price = show d ++ "."
++ if p < 10 then "0" ++ show p else show p
where d = price `div` 100
p = price `mod` 100
© M. Winter
3.9
COSC 4P41 – Functional Programming
Example: Supermarket billing (cont’d)
formatLine :: (Name,Price) -> String
formatLine (name,price) = name ++ replicate n '.' ++ ps ++ "\n"
where ps = formatPence price
n = lineLength - length name - length ps
formatLines :: BillType -> String
formatLines = concat . map formatLine
makeTotal :: BillType -> Price
makeTotal = sum . map snd
formatTotal
formatTotal
where ps
n
© M. Winter
:: Price -> String
price = "\nTotal" ++ replicate n '.' ++ ps
= formatPence price
= lineLength - 5 - length ps
3.10
COSC 4P41 – Functional Programming
Example: Supermarket billing (cont’d)
formatBill :: BillType -> String
formatBill bill = title ++ formatLines bill
++ formatTotal (makeTotal bill)
where title = replicate sp ' ' ++ "Haskell Stores\n\n"
sp
= (lineLength - 14) `div` 2
produceBill :: TillType -> String
produceBill = formatBill . makeBill
printBill :: TillType -> IO ()
printBill = putStr . produceBill
© M. Winter
3.11
COSC 4P41 – Functional Programming
Case Construction
firstDigit :: String -> Char
firstDigit st =
case (digits st) of
[]
-> ’\0’
(x:_) -> x
General form:
case e
p1
p2
...
pk
© M. Winter
of
-> e1
-> e2
-> ek
3.12
COSC 4P41 – Functional Programming
Primitive recursion over lists
sum :: [Int] -> Int
sum []
= 0
sum (x:xs)
= x + sum xs
General pattern:
fun []
fun (x:xs)
= …
= … x … xs … fun xs …
iSort :: [Int] -> [Int]
iSort []
= []
iSort (x:xs) = ins x (iSort xs)
where ins x []
= [x]
ins x z@(y:ys)
= if x<=y then x:z else y:ins x ys
© M. Winter
3.13
Related documents