Example picture
This presentation is the property of its rightful owner.
Sponsored Links
1 / 13

Example Picture PowerPoint PPT Presentation


  • 67 Views
  • Uploaded on
  • Presentation posted in: General

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

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Example picture

Example Picture

type Picture = [[Char]]

horse :: Picture

horse =

[[’.’,’.’,’.’,’.’,’.’,’.’,’.’,’#’,’#’,’.’,’.’,’.’],

[’.’,’.’,’.’,’.’,’.’,’#’,’#’,’.’,’.’,’#’,’.’,’.’],

[’.’,’.’,’.’,’#’,’#’,’.’,’.’,’.’,’.’,’.’,’#’,’.’],

[’.’,’.’,’#’,’.’,’.’,’.’,’.’,’.’,’.’,’.’,’#’,’.’],

[’.’,’.’,’#’,’.’,’.’,’.’,’#’,’.’,’.’,’.’,’#’,’.’],

[’.’,’.’,’#’,’.’,’.’,’.’,’#’,’#’,’#’,’.’,’#’,’.’],

[’.’,’#’,’.’,’.’,’.’,’.’,’#’,’.’,’.’,’#’,’#’,’.’],

[’.’,’.’,’#’,’.’,’.’,’.’,’#’,’.’,’.’,’.’,’.’,’.’],

[’.’,’.’,’.’,’#’,’.’,’.’,’.’,’#’,’.’,’.’,’.’,’.’],

[’.’,’.’,’.’,’.’,’#’,’.’,’.’,’#’,’.’,’.’,’.’,’.’],

[’.’,’.’,’.’,’.’,’.’,’#’,’.’,’#’,’.’,’.’,’.’,’.’],

[’.’,’.’,’.’,’.’,’.’,’.’,’#’,’#’,’.’,’.’,’.’,’.’]]


Example picture cont d

Example Picture (cont’d)

printPicture :: Picture -> IO ()

printPicture = putStr . concat . map (++"\n")

printPicture horse

.......##...

.....##..#..

...##.....#.

..#.......#.

..#...#...#.

..#...###.#.

.#....#..##.

..#...#.....

...#...#....

....#..#....

.....#.#....

......##....


Example picture cont d1

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


Local definitions

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 …


Example picture

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


Example supermarket billing

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


Example supermarket billing cont d

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


Example supermarket billing cont d1

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


Example supermarket billing cont d2

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


Example supermarket billing cont d3

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


Example supermarket billing cont d4

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


Case construction

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


Primitive recursion over lists

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


  • Login