pictures: a case study lecture 4, programmeringsteknik del a
DESCRIPTION
Top-Down Design Original problem Break into subproblems. Problems simple enough to solve with one function.TRANSCRIPT
![Page 1: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/1.jpg)
Pictures: A Case Study
Lecture 4,Programmeringsteknik del A.
![Page 2: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/2.jpg)
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.
![Page 3: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/3.jpg)
Top-Down DesignOriginalproblem
Break intosubproblems.
Problemssimple
enough tosolve with
one function.
![Page 4: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/4.jpg)
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.
![Page 5: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/5.jpg)
Bottom-Up Design
Interestingfunctions.
Interestingcombinations.
Desiredprogram.
![Page 6: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/6.jpg)
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!
![Page 7: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/7.jpg)
Interfaces
What can the userassume?
What must theimplementor 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.
![Page 8: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/8.jpg)
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?
![Page 9: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/9.jpg)
The Type of Pictures
Pictures are a kind of data; we will need a type for them.
type Picture = ...Not known yet.
Part of theinterface.
Private to theimplementation.
A type whose name is public, but whose representation is private, is called an abstract data type.
![Page 10: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/10.jpg)
Functions on Pictures
flipH
flipV
flipH, flipV :: Picture -> Picture
![Page 11: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/11.jpg)
Properties of Flipping
What properties should flipH, flipV satisfy?
•flipH (flipH pic) == pic
•flipV (flipV pic) == pic
•flipH (flipV pic) == flipV (flipH pic)
Properties whichthe user can assume.
Properties which theimplementor should guarantee.
![Page 12: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/12.jpg)
Combining Pictures
sideBySide
above
above, sideBySide ::
Picture -> Picture -> Picture
![Page 13: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/13.jpg)
Properties of Combinations
a `above` (b `above` c) == (a `above` b) `above` c
![Page 14: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/14.jpg)
Properties of Combinations
flipH (a `above` b) == flipH a `above` flipH b
![Page 15: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/15.jpg)
Properties of Combinations
flipV (a `above` b) == flipV b `above` flipV b
![Page 16: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/16.jpg)
Printing Pictures
What can we do with a picture once we’ve constructed it?
printPicture :: Picture -> IO ()
![Page 17: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/17.jpg)
Quiz: Using Pictures
white
black
check
![Page 18: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/18.jpg)
Quiz: Using Pictures
white
black
check
wb = white `sideBySide` black
check = wb `above` flipH wb
![Page 19: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/19.jpg)
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 withincheck.
![Page 20: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/20.jpg)
Quiz: Make a Chess Board
![Page 21: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/21.jpg)
Quiz: Make a Chess Board
quartet check
quartet (quartet check)
![Page 22: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/22.jpg)
Defining Quartet
quartet :: Picture -> Picture
quartet pic = two `above` two
where two = pic `sideBySide` pic
A local definition canuse the function arguments.
![Page 23: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/23.jpg)
Representing Pictures
We choose a representation which we can easily print from Hugs -- not beautiful, but simple.
## # # # # ######## ##
[” ## ”, ” # # ”, ” # # ”, ” ########”, ” ## ”]
type Picture = [String]
![Page 24: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/24.jpg)
The Truth About Strings
A string is a sequence of characters.
type String = [Char]
Examples:
’a’, ’b’, ’c’ :: Char
’ ’ :: Char
The type ofcharacters.
A space is alsoa character.
![Page 25: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/25.jpg)
Flipping Vertically
[” ## ”, ” # # ”, ” # # ”, ” ########”, ” ## ”]
flipV
[” ## ”, ” ########”, ” # # ”, ” # # ”, ” ## ”]
flipV :: Picture -> Picture
flipV pic = reverse pic
![Page 26: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/26.jpg)
Quiz: Flipping Horizontally
flipH
[” # ”, ” # ”, ”######”, ” # ”, ” # ”]
[” # ”, ” # ”, ”######”, ” # ”, ” # ”]
Recall: Picture = [[Char]]
![Page 27: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/27.jpg)
Quiz: Flipping Horizontally
flipH
[” # ”, ” # ”, ”######”, ” # ”, ” # ”]
[” # ”, ” # ”, ”######”, ” # ”, ” # ”]
flipH :: Picture -> Picture
flipH pic = [reverse line | line <- pic]
![Page 28: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/28.jpg)
Combining Pictures Vertically[” # ”, ” # ”, ”######”, ” # ”, ” # ”]
[” # ”, ” # ”, ”######”, ” # ”, ” # ”]
[” # ”, ” # ”, ”######”, ” # ”, ” # ”, ” # ”, ” # ”, ”######”, ” # ”, ” # ”]
above
above p q = p ++ q
![Page 29: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/29.jpg)
Combining Pictures Horizontally[” # ”, ” # ”, ”######”, ” # ”, ” # ”]
[” # ”, ” # ”, ”######”, ” # ”, ” # ”]
`sideBySide`
[” # # ”, ” # # ”, ”############”, ” # # ”, ” # # ”]
![Page 30: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/30.jpg)
Combining Pictures Horizontally[” # ”, ” # ”, ”######”, ” # ”, ” # ”]
[” # ”, ” # ”, ”######”, ” # ”, ” # ”]
`sideBySide`
sideBySide :: Picture -> Picture -> Picture
p `sideBySide` q = [pline ++ qline | (pline,qline) <- zip p q]
![Page 31: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/31.jpg)
Printing
We will need to be able to print strings. Haskell provides a command to do so.
putStr :: String -> IO ()
A command with noresult. Executing thecommand prints the
string.
Main> putStr ”Hello!”
Hello!
Main>
Main> ”Hello!”
”Hello!”
Main>Executed thecommand.
Displayedthe value.
![Page 32: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/32.jpg)
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>
![Page 33: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/33.jpg)
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)
![Page 34: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/34.jpg)
Extension: Superimposing Pictures
superimpose
[” # ”, ” # ”, ”######”, ” # ”, ” # ”]
[” # ”, ” # ”, ”######”, ” # ”, ” # ”]
[” ## ”, ” # # ”, ”######”, ” # # ”, ” ## ”]
![Page 35: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/35.jpg)
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 twocases without using
a guard.
![Page 36: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/36.jpg)
Can We Solve a Simpler Problem?
We can superimpose two lines:
superimposeLine :: String -> String -> StringsuperimposeLine s s’ =
[superimposeChar c c’ | (c,c’) <- zip s s’]
superimposeLine ”# ” ” #”[superimposeChar ’#’ ’ ’, superimposeChar ’ ’ ’ ’, superimposeChar ’ ’ ’#’]
”# #”
![Page 37: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/37.jpg)
Superimposing Pictures
superimpose :: Picture -> Picture -> Picture
superimpose p q =
[superimposeLine pline qline | (pline, qline) <- zip p q]
![Page 38: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/38.jpg)
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
![Page 39: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/39.jpg)
Defining the Interface
module Pictures(Picture, flipV, flipH, above, sideBySide, printPicture) where
…
These are the definitions whichother programs
may use.
Other definitions mayappear here, but they are
private.
![Page 40: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/40.jpg)
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.
![Page 41: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/41.jpg)
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!
![Page 42: Pictures: A Case Study Lecture 4, Programmeringsteknik del A](https://reader036.vdocuments.site/reader036/viewer/2022081512/5a4d1b0e7f8b9ab05998d820/html5/thumbnails/42.jpg)
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.