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

Slides:



Advertisements
Similar presentations
Today’s Objectives: Students will be able to:
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
Chapter 3 Simple Graphics. Side Effects and Haskell  All the programs we have seen so far have no “side-effects.” That is, programs are executed only.
Fall 2007CS 225 Introduction to Software Design Chapter 1.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
CSSE221: Software Dev. Honors Day 10 Announcements Announcements Fifteen due Monday 11:59 pm. Monday will be a workday. Fifteen due Monday 11:59 pm. Monday.
Introduction to Software Design Chapter 1. Chapter 1: Introduction to Software Design2 Chapter Objectives To become familiar with the software challenge.
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Feb. 23, 2004CS WPI1 CS 509 Design of Software Systems Lecture #5 Monday, Feb. 23, 2004.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Guide To UNIX Using Linux Third Edition
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called.
Chapter 2: Algorithm Discovery and Design
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Introducing Java.
Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming.
Functional Programming in Haskell Motivation through Concrete Examples Adapted from Lectures by Simon Thompson.
Tuples and Lists Lecture 3, Programmeringsteknik del A.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
The Data Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects.
1 Summary: Design Methods for Algorithms Andreas Klappenecker.
Fundamentals of Python: First Programs
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Introduction to programming in the Java programming language.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
Algorithms and Programming Functions Lecture 28. Summary of Previous Lecture while statement for statement break statement Nested loops.
Chapter 3 Object Interaction.  To construct interesting applications it is not enough to build individual objects  Objects must be combined so they.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions.
Function Definition by Cases and Recursion Lecture 2, Programmeringsteknik del A.
Data Structure Introduction Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2010.
Chapter 10 Algorithmic Thinking. Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the.
Case Study: Route Finding Lecture 6, Programmeringsteknik del A.
Chapter 1 Data Abstraction: The Walls CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Halting Measures and Termination Arguments CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
CSci 162 Lecture 8 Martin van Bommel. Large-scale Programming Up to now we have been writing relatively short programs to solve simple problem Want to.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under.
Evolution of C and C++ n C was developed by Dennis Ritchie at Bell Labs (early 1970s) as a systems programming language n C later evolved into a general-purpose.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Algorithms and Problem Solving
CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.7
CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1
Chapter 4: Writing Classes
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Chapter 6 Methods: A Deeper Look
Solving Your Problem by Generalization
Unit 3: Lesson 6 & 7- Functions and Top-Down Design / APIs and Function Parameters Day 27.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Presentation transcript:

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

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.

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

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.

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

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!

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.

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?

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.

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

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.

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

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

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

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

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

Quiz: Using Pictures white black check

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

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.

Quiz: Make a Chess Board

quartet check quartet (quartet check)

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

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

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.

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

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

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

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

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

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

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.

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>

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)

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

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.

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 ’ ’ ’#’] ”# #”

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

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

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.

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.

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!

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.