1 / 13

Example Picture

Example Picture. type Picture = [[Char]] horse :: Picture horse = [[’.’,’.’,’.’,’.’,’.’,’.’,’.’,’#’,’#’,’.’,’.’,’.’], [’.’,’.’,’.’,’.’,’.’,’#’,’#’,’.’,’.’,’#’,’.’,’.’], [’.’,’.’,’.’,’#’,’#’,’.’,’.’,’.’,’.’,’.’,’#’,’.’], [’.’,’.’,’#’,’.’,’.’,’.’,’.’,’.’,’.’,’.’,’#’,’.’],

bob
Download Presentation

Example Picture

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Example Picture type Picture = [[Char]] horse :: Picture horse = [[’.’,’.’,’.’,’.’,’.’,’.’,’.’,’#’,’#’,’.’,’.’,’.’], [’.’,’.’,’.’,’.’,’.’,’#’,’#’,’.’,’.’,’#’,’.’,’.’], [’.’,’.’,’.’,’#’,’#’,’.’,’.’,’.’,’.’,’.’,’#’,’.’], [’.’,’.’,’#’,’.’,’.’,’.’,’.’,’.’,’.’,’.’,’#’,’.’], [’.’,’.’,’#’,’.’,’.’,’.’,’#’,’.’,’.’,’.’,’#’,’.’], [’.’,’.’,’#’,’.’,’.’,’.’,’#’,’#’,’#’,’.’,’#’,’.’], [’.’,’#’,’.’,’.’,’.’,’.’,’#’,’.’,’.’,’#’,’#’,’.’], [’.’,’.’,’#’,’.’,’.’,’.’,’#’,’.’,’.’,’.’,’.’,’.’], [’.’,’.’,’.’,’#’,’.’,’.’,’.’,’#’,’.’,’.’,’.’,’.’], [’.’,’.’,’.’,’.’,’#’,’.’,’.’,’#’,’.’,’.’,’.’,’.’], [’.’,’.’,’.’,’.’,’.’,’#’,’.’,’#’,’.’,’.’,’.’,’.’], [’.’,’.’,’.’,’.’,’.’,’.’,’#’,’#’,’.’,’.’,’.’,’.’]]

  2. Example Picture (cont’d) printPicture :: Picture -> IO () printPicture = putStr . concat . map (++"\n") printPicture horse .......##... .....##..#.. ...##.....#. ..#.......#. ..#...#...#. ..#...###.#. .#....#..##. ..#...#..... ...#...#.... ....#..#.... .....#.#.... ......##....

  3. 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

  4. 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 …

  5. 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

  6. 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

  7. 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)]

  8. 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

  9. 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

  10. 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 :: Price -> String formatTotal price = "\nTotal" ++ replicate n '.' ++ ps where ps = formatPence price n = lineLength - 5 - length ps

  11. 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

  12. Case Construction firstDigit :: String -> Char firstDigit st = case (digits st) of [] -> ’\0’ (x:_) -> x General form: case e of p1 -> e1 p2 -> e2 ... pk -> ek

  13. 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

More Related