pictures, a case study(4)

Upload: nikhil-sahu

Post on 05-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Pictures, A Case Study(4)

    1/42

    Pictures: A Case Study

    Lecture 4,

    Programmeringsteknik del A.

  • 7/31/2019 Pictures, A Case Study(4)

    2/42

    Purpose

    We will go through a larger example of designing a part of a

    program: a library of functions for manipulating pictures.

    Why?

    See examples of programming with lists; practice what we

    have learned.

    Introduce the type Char of characters.

    Discuss the design of larger parts of programs than individual

    functions.

  • 7/31/2019 Pictures, A Case Study(4)

    3/42

    Top-Down Design

    Original

    problemBreak into

    subproblems.

    Problems

    simple

    enough to

    solve with

    one function.

  • 7/31/2019 Pictures, A Case Study(4)

    4/42

    Top-Down Design

    What functions might help me solve this problem?

    Break down a difficult problem into manageable pieces -- an

    effective problem solving strategy.

    Work is directed towards solving the problem in hand -- nounnecessary programming.

    May not result in many generally useful functions as a by-

    product.

  • 7/31/2019 Pictures, A Case Study(4)

    5/42

    Bottom-Up Design

    Interesting

    functions.

    Interesting

    combinations.

    Desired

    program.

  • 7/31/2019 Pictures, A Case Study(4)

    6/42

    Bottom-Up Design

    What can I do with the functions available to me?

    An effective way to build up powerful program components.

    Work is directed towards producing generally useful

    functions, which can then be reused in many applications.

    A solution looking for a problem!

  • 7/31/2019 Pictures, A Case Study(4)

    7/42

    Interfaces

    What can the user

    assume?

    What must the

    implementor provide?

    An interface defines the `contract

    between two program components.

    The implementation can be replaced by

    any other that respects the interface.

    Cf. Renting a

    car.

  • 7/31/2019 Pictures, A Case Study(4)

    8/42

    Example: Pictures

    Top-down: We have discovered a need to manipulate

    rectangular pictures.

    Bottom-up: What interesting functions can we provide?

    Interface: What should the `contract between pictureusers and the picture implementor specify?

  • 7/31/2019 Pictures, A Case Study(4)

    9/42

    The Type of Pictures

    Pictures are a kind of data; we will need a type for them.

    type Picture = ...

    Not known yet.

    Part of the

    interface.

    Private to the

    implementation.

    A type whose name is public, but whose representation is

    private, is called an abstract data type.

  • 7/31/2019 Pictures, A Case Study(4)

    10/42

    Functions on Pictures

    flipH

    flipV

    flipH, flipV :: Picture -> Picture

  • 7/31/2019 Pictures, A Case Study(4)

    11/42

    Properties of Flipping

    What properties should flipH, flipV satisfy?

    flipH (flipH pic) == pic

    flipV (flipV pic) == pic

    flipH (flipV pic) == flipV (flipH pic)

    Properties which

    the user can assume.

    Properties which the

    implementor should guarantee.

  • 7/31/2019 Pictures, A Case Study(4)

    12/42

    Combining Pictures

    sideBySide

    above

    above, sideBySide ::

    Picture -> Picture -> Picture

  • 7/31/2019 Pictures, A Case Study(4)

    13/42

    Properties of Combinations

    a `above` (b `above` c) == (a `above` b) `above` c

  • 7/31/2019 Pictures, A Case Study(4)

    14/42

    Properties of Combinations

    flipH (a `above` b) == flipH a `above` flipH b

  • 7/31/2019 Pictures, A Case Study(4)

    15/42

    Properties of Combinations

    flipV (a `above` b) == flipV b `above` flipV b

  • 7/31/2019 Pictures, A Case Study(4)

    16/42

  • 7/31/2019 Pictures, A Case Study(4)

    17/42

    Quiz: Using Pictures

    white

    black

    check

  • 7/31/2019 Pictures, A Case Study(4)

    18/42

    Quiz: Using Pictures

    white

    black

    check

    wb = white `sideBySide` black

    check = wb `above` flipH wb

  • 7/31/2019 Pictures, A Case Study(4)

    19/42

    Local Definitions

    Wb is used only to define check. But when we read the

    program, we cant tell.

    check = wb `above` flipH wb

    where wb = white `sideBySide` black

    Only defined within

    check.

  • 7/31/2019 Pictures, A Case Study(4)

    20/42

    Quiz: Make a Chess Board

  • 7/31/2019 Pictures, A Case Study(4)

    21/42

    Quiz: Make a Chess Board

    quartet check

    quartet (quartet check)

  • 7/31/2019 Pictures, A Case Study(4)

    22/42

    Defining Quartet

    quartet :: Picture -> Picture

    quartet pic = two `above` two

    where two = pic `sideBySide` pic

    A local definition can

    use the function arguments.

  • 7/31/2019 Pictures, A Case Study(4)

    23/42

  • 7/31/2019 Pictures, A Case Study(4)

    24/42

  • 7/31/2019 Pictures, A Case Study(4)

    25/42

    Flipping Vertically

    [ ## ,

    # # ,

    # # ,

    ########,

    ## ]

    flipV

    [ ## ,

    ########,

    # # ,

    # # ,

    ## ]

    flipV :: Picture -> Picture

    flipV pic = reverse pic

  • 7/31/2019 Pictures, A Case Study(4)

    26/42

    Quiz: Flipping Horizontally

    flipH

    [ # ,

    # ,

    ######, # ,

    # ]

    [ # ,

    # ,

    ######, # ,

    # ]

    Recall: Picture = [[Char]]

  • 7/31/2019 Pictures, A Case Study(4)

    27/42

    Quiz: Flipping Horizontally

    flipH

    [ # ,

    # ,

    ######, # ,

    # ]

    [ # ,

    # ,

    ######, # ,

    # ]

    flipH :: Picture -> Picture

    flipH pic = [reverse line | line

  • 7/31/2019 Pictures, A Case Study(4)

    28/42

    Combining Pictures Vertically

    [ # ,

    # ,

    ######,

    # ,

    # ]

    [ # ,

    # ,

    ######, # ,

    # ]

    [ # ,

    # ,

    ######,

    # ,

    # ,

    # ,

    # ,

    ######,

    # , # ]

    above

    above p q = p ++ q

  • 7/31/2019 Pictures, A Case Study(4)

    29/42

    Combining Pictures Horizontally

    [ # ,

    # ,

    ######,

    # ,

    # ]

    [ # ,

    # ,

    ######,

    # ,

    # ]

    `sideBySide`

    [ # # ,

    # # ,############,

    # # ,

    # # ]

  • 7/31/2019 Pictures, A Case Study(4)

    30/42

    Combining Pictures Horizontally

    [ # ,

    # ,

    ######,

    # ,

    # ]

    [ # ,

    # ,

    ######,

    # ,

    # ]

    `sideBySide`

    sideBySide :: Picture -> Picture -> Picture

    p `sideBySide` q = [pline ++ qline | (pline,qline)

  • 7/31/2019 Pictures, A Case Study(4)

    31/42

  • 7/31/2019 Pictures, A Case Study(4)

    32/42

  • 7/31/2019 Pictures, A Case Study(4)

    33/42

  • 7/31/2019 Pictures, A Case Study(4)

    34/42

    Extension: Superimposing

    Pictures

    superimpose

    [ # ,

    # ,

    ######,

    # ,

    # ]

    [ # ,

    # ,

    ######,

    # ,

    # ]

    [ ## ,

    # # ,

    ######,

    # # ,

    ## ]

  • 7/31/2019 Pictures, A Case Study(4)

    35/42

  • 7/31/2019 Pictures, A Case Study(4)

    36/42

    Can We Solve a Simpler

    Problem?We can superimpose two lines:

    superimposeLine :: String -> String -> String

    superimposeLine s s =

    [superimposeChar c c | (c,c)

  • 7/31/2019 Pictures, A Case Study(4)

    37/42

    Superimposing Pictures

    superimpose :: Picture -> Picture -> Picture

    superimpose p q =

    [superimposeLine pline qline | (pline, qline)

  • 7/31/2019 Pictures, A Case Study(4)

    38/42

  • 7/31/2019 Pictures, A Case Study(4)

    39/42

  • 7/31/2019 Pictures, A Case Study(4)

    40/42

    Lessons

    Two problem solving strategies:

    Top-down: break a problem into smaller subproblems.

    Bottom-up: combine solutions into bigger solutions.

    Collect useful related functions (often based around a common

    type) into libraries for future use.

  • 7/31/2019 Pictures, A Case Study(4)

    41/42

  • 7/31/2019 Pictures, A Case Study(4)

    42/42