Lab 11 Lab 1: CSG 711: Programming to Structure Karl Lieberherr.

Slides:



Advertisements
Similar presentations
Higher-Order Functions and Loops c. Kathi Fisler,
Advertisements

Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
Scheme: Functions Chapter 3 of HTDP Ms. Knudtzon September 12.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
MATH 224 – Discrete Mathematics
Lab1 Lab 1: CSG 711: Programming to Structure Karl Lieberherr.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Algorithms + L. Grewe.
Loops in Scheme, II c. Kathi Fisler, 2001 (early slides assume map/filter)
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Chapter 2: Algorithm Discovery and Design
CS 201 Functions Debzani Deb.
Main task -write me a program
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Introducing General Recursion CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Mutually-Recursive Data Definitions CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.4 © Mitchell Wand, This work is licensed under a Creative.
Lab1 Lab 1: CSG 711: Programming to Structure Karl Lieberherr.
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
Introduction to Programming Lecture Number:. What is Programming Programming is to instruct the computer on what it has to do in a language that the computer.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
6 Dec 2001Kestrel1 Teaching with Patterns Matthias Felleisen Daniel Jackson.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Programming Paradigms Procedural Functional Logic Object-Oriented.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.5 © Mitchell Wand, This work is licensed under a Creative.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
TeachScheme, ReachJava Adelphi University Wednesday morning June 24, 2008.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
CS 603: Programming Language Organization Lecture 10 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 8: Accumulating.
C. Varela; Adapted w. permission from S. Haridi and P. Van Roy1 Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections ,
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Data Abstraction: Sets Binary Search Trees CMSC Introduction to Computer Programming October 30, 2002.
Cs1321 December 6, 2001 Review. What is computer science? What's an algorithm? Processes and programs Overview of some programming language concepts Functional.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Introducing General Recursion CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you.
Halting Measures and Termination Arguments CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
A Recipe for Algorithm Design Or, What to do when the data type doesn't match the function…yet.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Functional Programming Language 1 Scheme Language: part 3.
Functional Programming
CS 550 Programming Languages Jeremy Johnson
Functional Programming
Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections , 3.2, , 4.7.2) Carlos Varela RPI September 12,
Integer Programming An integer linear program (ILP) is defined exactly as a linear program except that values of variables in a feasible solution have.
Class 11: Two-argument recursion
6.001 SICP Variations on a Scheme
CS 326 Programming Languages, Concepts and Implementation
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
CS 36 – Chapter 11 Functional programming Features Practice
A Case Study: Space Invaders
Let's Play "What's the Question"
Announcements Quiz 5 HW6 due October 23
6.001 SICP Interpretation Parts of an interpreter
To understand recursion, one must first understand recursion.
Rewriting your function using map and foldr
A Case Study: Space Invaders
Presentation transcript:

Lab 11 Lab 1: CSG 711: Programming to Structure Karl Lieberherr

Lab 12 Using Dr. Scheme context-sensitive help use F1 as your mouse is on an identifier. HelpDesk is language sensitive. Be patient. try the stepper develop programs incrementally definition and use: Check Syntax use 299 / intermediate with lambda

Lab 13 General Recipe Write down the requirements for a function in a suitable mathematical notation. Structural design recipe: page 368/369 HtDP

Lab 14 Designing Algorithms Data analysis and design Contract, purpose header Function examples Template Definition –what is a trivially solvable problem? –what is a corresponding solution? –how do we generate new problems –need to combine solutions of subproblems Test

Lab 15 Template (define (generative-rec-fun problem) (cond [(trivially-solvable? problem) (determine-solution problem)] [else (combine-solutions … problem … (generative-rec-fun (gen-pr-1 problem)) … (generative-rec-fun (gen-pr-n problem)))]))

Lab 16 Template for list-processing (define (generative-rec-fun problem) (cond [(empty? problem) (determine-solution problem)] [else (combine-solutions problem (generative-rec-fun (rest problem)))]))

Lab 17 duple (EOPL page 24) (duple n x) li:= empty; for i :=1 to n do add x to li (does not matter where); Structural recursion: if i=0 empty else (cons x (duple (- n 1))

Lab 18 History (Programming to Structure) Frege: Begriffsschrift 1879: “The meaning of a phrase is a function of the meanings of its immediate constituents.” Example: class dictionary AL AppleList : Mycons | Myempty. Mycons = Apple AppleList. Apple = int. Myempty =.

Lab 19 Program Design Recipe 1.Problem Analysis & Data Definition 1.Class dictionary AL 2.Contract, Purpose, Effect Statements, Header 1.in tWeight(AppleList al): sums weight of all apples 3.Examples 1.apples (2 4 6) => 12 4.Function Template 1.determined by AL 5.Function Definition 6.Tests

Lab 110 Meaning of a list of apples? Total weight (tWeight al) –[(Myempty? al) 0] –[(Mycons? al) (Apple-weight(Mycons-first al)) // meaning of first constituent + (tWeight(Mycons-rest al))] // meaning of rest constituent AppleList : Mycons | Myempty. Mycons = Apple AppleList. Apple = int. Myempty =. PL independent AppleList Mycons Myempty Apple int rest first weight

Lab 111 In Scheme: Structure (define-struct Mycons (first rest)) (define-struct Apple (weight)) (define-struct Myempty ())

Lab 112 Design Information AppleList : Mycons | Myempty. Mycons = Apple AppleList. Apple = int. Myempty =. AppleList Mycons Myempty Apple int rest first weight Scheme solution Mycons Myempty Apple int rest first weight (define-struct Mycons (first rest)) (define-struct Apple (weight)) (define-struct Myempty ())

Lab 113 In Scheme: Behavior (define (tWeight al) (cond [(Myempty? al) 0] [(Mycons? al) (+ (Apple-weight (Mycons-first al)) (tWeight (Mycons-rest al)))]))

Lab 114 In Scheme: Testing (define list1 (make-Mycons (make-Apple 111) (make-Myempty))) (tWeight list1) 111 (define list2 (make-Mycons (make-Apple 50) list1)) (tWeight list2) 161 Note: A test should return a Boolean value. See tutorial by Alex Friedman on testing in Dr. Scheme.

Lab 115 Reflection on Scheme solution Program follows structure Design translated somewhat elegantly into program. Dynamic programming language style. But the solution has problems! –duplicates structure in data (define list2 (make-Mycons (make-Apple 50) list1)) apples (50 …) –duplicates structure in function

Lab 116 Behavior While the purpose of this lab is programming to structure, the Scheme solution duplicates the structure! (define (tWeight al) (cond [(Myempty? al) 0] [(Mycons? al) (+ (Apple-weight (Mycons-first al)) (tWeight (Mycons-rest al)))])) duplicates all of it!

Lab 117 How can we reduce the duplication of structure? First small step: Express all of structure in programming language once. Eliminate conditional! Implementation of tWeight() has a method for Mycons and Myempty. Extensible by addition not modification. Big win of OO.

Lab 118 Solution in Java AppleList: abstract int tWeight(); Mycons: int tWeight() { return (first.tWeight() + rest.tWeight()); } Myempty: int tWeight() {return 0;} AppleList : Mycons | Myempty. Mycons = Apple AppleList. Apple = int. Myempty =. + translated to Java

Lab 119 What is better? structure-shyness has improved. No longer enumerate alternatives in functions. Better follow principle of single point of control (of structure).

Lab 120 Problem to think about (while you do hw 1) Consider the following two Shape definitions. –in the first, a combination consists of exactly two shapes. –in the other, a combination consists of zero or more shapes. Is it possible to write a program that works correctly for both shape definitions?

Lab 121 First Shape Shape : Rectangle | Circle | Combination. Rectangle = "rectangle" int int int int. Circle = "circle" int int int. Combination = "(" Shape Shape ")".

Lab 122 Second Shape Shape : Rectangle | Circle | Combination. Rectangle = "rectangle" int int int int. Circle = "circle" int int int. Combination = "(" List(Shape) ")". List(S) ~ {S}.

Lab 123 Input (for both Shapes) ( rectangle ( circle rectangle )

Lab 124 Think of a shape as a list! A shape is a list of rectangles and circles. Visit the elements of the list to solve the area, inside and bounding box problems.

Lab 125 Help with the at function Design the function at. It consumes a set S and a relation R. Its purpose is to collect all the seconds from all tuples in R whose first is a member of S.

Lab 126 Deriving Scheme solution (1) at: s: Set r: Relation Set s0 = {}; from r:Relation to p:Pair: if (p.first in s) s0.add(p.second); return s0; at: s: Set r: Relation if empty(r) return empty set else { Set s0 = {}; p1 := r.first(); if (p1.first in s) s0.add(p1.second); return union(s0, at(s, rest(r))} definition decompose based on structure of a relation: either it is empty or has a first element

Lab 127 Deriving Scheme solution (2) at: s: Set r: Relation Set s0 = {}; from r:Relation to p:Pair: if (p.first in s) s0.add(p.second); return s0; at: s: Set r: Relation if empty(r) return empty set else { p1 := r.first(); rst = at(s, rest(r)); if (p1.first in s) return rst.add(p1.second) else rst} definition decompose based on structure of a relation: either it is empty or has a first element Why not implement this definition directly using iteration ???

Lab 128 Close to final solution ;; at : Symbol Relation -> Set (define (at s R) (cond [(empty? R) empty-set] [else (local ((define p1 (first R)) (define rst (at s (rest R)))) (if (element-of (first p1) s) (add-element (second p1) rst) rst))])) at: s: Set r: Relation if empty(r) return empty set else { p1 := r.first(); rst = at(s, rest(r)); if (p1.first in s) return rst.add(p.second) else rst}

Lab 129 dot example Compute the composition of two relations. r and s are relations. r.s (dot r s) is the relation t such that x t z holds iff there exists a y so that x r y and y s z.

Lab 130 Why not implement iterative solution? dot Relation r1, r2 Relation r0 = {}; from r1: Relation to p1: Pair from r2: Relation to p2: Pair if (= p1.second p2.first) r0.add( new Pair(p1.first,p2.second)); return r0; if empty(r1) return empty-set else ;; there must be a first element p11 in r1 Relation r0 = empty-set; from r2: Relation to p2: Pair if (= p11.second p2.first) r0.add(new Pair(p11.first,p2.second)); return union (r0, dot((rest r1),r2));

Lab 131 Closer to Scheme solution: reuse at dot Relation r, s; if empty(r) return empty-set else ;; there must be a first element fst in r x=fst.first; y=fst.second; zs = at(list(y), s); turn x and zs into list of pairs: r0; return union (r0, dot((rest r),s));

Lab 132 Scheme solution (define (dot.v0 r s) (cond [(empty? r) empty] [else (local ((define fst (first r)) (define x (first fst)) (define y (second fst)) (define zs (at (list y) s))) (union (map (lambda (s) (list x s)) zs) (dot.v0 (rest r) s)))]))

Lab 133 Save for later

Lab 134 Abstractions abstraction through parameterization: – planned modification points aspect-oriented abstractions: –unplanned extension points

Lab 135 Structure The Scheme program has lost information that was available at design time. –The first line is missing in structure definition. –Scheme allows us to put anything into the fields. AppleList : Mycons | Myempty. Mycons = Apple AppleList. Apple = int. Myempty =.

Lab 136 Information can be expressed in Scheme Dynamic tests Using object system

Lab 137 Program Design Recipe 1.Problem Analysis & Data Definition 2.Contract, Purpose, Effect Statements, Header 3.Examples 4.Function Template 5.Function Definition 6.Tests

Lab 138 Program Design Recipe 1.Problem Analysis & Data Definition 2.Contract, Purpose, Effect Statements, Header 3.Examples 4.Function Template 5.Function Definition 6.Tests

Lab 139 Guideline on Auxiliary Functions Formulate auxiliary function definitions for every dependency between quantities mentioned in the problem statement or discovered with example computations. The first and most important guideline of programming (page 25 HtDP).