Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pictures: A Case Study Lecture 4, Programmeringsteknik del A.

Similar presentations


Presentation on theme: "Pictures: A Case Study Lecture 4, Programmeringsteknik del A."— Presentation transcript:

1 Pictures: A Case Study Lecture 4, Programmeringsteknik del A.

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

3 Top-Down Design Original problem Break into subproblems. Problems simple enough to solve with one function.

4 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 -- no unnecessary programming. May not result in many generally useful functions as a by- product.

5 Bottom-Up Design Interesting functions. Interesting combinations. Desired program.

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

8 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 picture users and the picture implementor specify?

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

10 Functions on Pictures flipH flipV flipH, flipV :: Picture -> Picture

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

12 Combining Pictures sideBySide above above, sideBySide :: Picture -> Picture -> Picture

13 Properties of Combinations a `above` (b `above` c) == (a `above` b) `above` c

14 Properties of Combinations flipH (a `above` b) == flipH a `above` flipH b

15 Properties of Combinations flipV (a `above` b) == flipV b `above` flipV b

16 Printing Pictures What can we do with a picture once we’ve constructed it? printPicture :: Picture -> IO ()

17 Quiz: Using Pictures white black check

18 Quiz: Using Pictures white black check wb = white `sideBySide` black check = wb `above` flipH wb

19 Local Definitions Wb is used only to define check. But when we read the program, we can’t tell. check = wb `above` flipH wb where wb = white `sideBySide` black Only defined within check.

20 Quiz: Make a Chess Board

21 quartet check quartet (quartet check)

22 Defining Quartet quartet :: Picture -> Picture quartet pic = two `above` two where two = pic `sideBySide` pic A local definition can use the function arguments.

23 Representing Pictures We choose a representation which we can easily print from Hugs -- not beautiful, but simple. ## ######## ## [” ## ”, ” # # ”, ” ######## ”, ” ## ”] type Picture = [String]

24 The Truth About Strings A string is a sequence of characters. type String = [Char] Examples: ’a’, ’b’, ’c’ :: Char ’ ’ :: Char The type of characters. A space is also a character.

25 Flipping Vertically [” ## ”, ” # # ”, ” ######## ”, ” ## ”] flipV [” ## ”, ” ######## ”, ” # # ”, ” ## ”] flipV :: Picture -> Picture flipV pic = reverse pic

26 Quiz: Flipping Horizontally flipH [” # ”, ” # ”, ”######”, ” # ”, ” # ”] [” # ”, ” # ”, ”######”, ” # ”, ” # ”] Recall:Picture = [[Char]]

27 Quiz: Flipping Horizontally flipH [” # ”, ” # ”, ”######”, ” # ”, ” # ”] [” # ”, ” # ”, ”######”, ” # ”, ” # ”] flipH :: Picture -> Picture flipH pic = [reverse line | line <- pic]

28 Combining Pictures Vertically [” # ”, ” # ”, ”######”, ” # ”, ” # ”] [” # ”, ” # ”, ”######”, ” # ”, ” # ”] [” # ”, ” # ”, ”######”, ” # ”, ”######”, ” # ”, ” # ”] above above p q = p ++ q

29 Combining Pictures Horizontally [” # ”, ” # ”, ”######”, ” # ”, ” # ”] [” # ”, ” # ”, ”######”, ” # ”, ” # ”] `sideBySide` [” # # ”, ” # # ”, ”############”, ” # # ”, ” # # ”]

30 Combining Pictures Horizontally [” # ”, ” # ”, ”######”, ” # ”, ” # ”] [” # ”, ” # ”, ”######”, ” # ”, ” # ”] `sideBySide` sideBySide :: Picture -> Picture -> Picture p `sideBySide` q = [pline ++ qline | (pline,qline) <- zip p q]

31 Printing We will need to be able to print strings. Haskell provides a command to do so. putStr :: String -> IO () A command with no result. Executing the command prints the string. Main> putStr ”Hello!” Hello! Main> Main> ”Hello!” ”Hello!” Main> Executed the command. Displayed the value.

32 Line Breaks How do we print more than one line? The special character ´\n´ indicates the end of a line. Main> putStr ”Hello\nWorld” Hello World Main> Main> ”Hello\nWorld” ”Hello\nWorld” Main>

33 Printing a Picture Print as ”###\n# #\n###\n” [”###”, ”# #”, ”###”] A common operation, so there is a standard function unlines :: [String] -> String printPicture :: Picture -> IO () printPicture pic = putStr (unlines pic)

34 Extension: Superimposing Pictures superimpose [” # ”, ” # ”, ”######”, ” # ”, ” # ”] [” # ”, ” # ”, ”######”, ” # ”, ” # ”] [” ## ”, ” # # ”, ”######”, ” # # ”, ” ## ”]

35 Can We Solve a Simpler Problem? We can superimpose two characters: superimposeChar :: Char -> Char -> Char superimposeChar c c’ = if c==’#’ then ’#’ else c’ Choose between two cases without using a guard.

36 Can We Solve a Simpler Problem? We can superimpose two lines: superimposeLine :: String -> String -> String superimposeLine s s’ = [superimposeChar c c’ | (c,c’) <- zip s s’] superimposeLine ” # ” ” # ” [superimposeChar ’#’ ’ ’, superimposeChar ’ ’ ’ ’, superimposeChar ’ ’ ’#’] ”# #”

37 Superimposing Pictures superimpose :: Picture -> Picture -> Picture superimpose p q = [superimposeLine pline qline | (pline, qline) <- zip p q]

38 Function Libraries The picture functions may be useful in many different programs. Instead of copying them into each one, we place them together in a library which other programs may include. module Pictures where type Picture = … flipV pic =... Pictures.hs import Pictures … flipV … flipH … Other.hs

39 Defining the Interface module Pictures (Picture, flipV, flipH, above, sideBySide, printPicture) where … These are the definitions which other programs may use. Other definitions may appear here, but they are private.

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

41 Lessons An interface makes explicit what the library author guarantees, and what the library user may assume. Specifies at least the types of functions, perhaps also properties and specifications. A library with a clear interface may be replaced by any other library with the same interface -- permits later improvements. Example: pictures with nicer graphics. Another demonstration of the power of list programming!

42 Reading The lecture is heavily based on Chapter 6 of the book: sections 6.1 and 6.3. Section 6.2 discusses an interesting extension of the picture example.


Download ppt "Pictures: A Case Study Lecture 4, Programmeringsteknik del A."

Similar presentations


Ads by Google