Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section 2.2.3 October 2008.

Slides:



Advertisements
Similar presentations
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Advertisements

Programming with Lists
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
6.001 SICP SICP – October Trees Trevor Darrell 32-D512 Office Hour: W web page:
6.001 SICP SICP – October HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W web page:
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.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
CS 330 Programming Languages 11 / 18 / 2008 Instructor: Michael Eckmann.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Scheme & Functional Programming. ( ) >> 64 ( ) >> 666 (* ) >> 1200 (+ (* 3 5) (- 10 6)) >> 19.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
Practice session #6: 1. Sequence operations 2. Partial evaluation with currying 3. Lazy-lists.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
Today’s topic: Abstraction Compound Data Data Abstractions: Isolate use of data abstraction from details of implementation Relationship between data abstraction.
Functional Programming in Scheme and Lisp.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
Fall 2008Programming Development Techniques 1 Topic 14 Multiple Representations of Abstract Data – Data Directed Programming and Additivity Section
PPL Lazy Lists. Midterm 2012 (define sum-vals (λ (ts) (if (ts-simple? ts) (ts-val ts) (accumulate + 0 (map ts-val (ts-inner-slots ts))))))
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
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.
CS 330 Programming Languages 11 / 28 / 2006 Instructor: Michael Eckmann.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 6. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
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.
Additional Scheme examples
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Class 11: Two-argument recursion
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
Racket CSC270 Pepper major portions credited to
Expanded Recursive Diagrams OCAML rapid tour, day 2
COP4020 Programming Languages
Env. Model Implementation
6.001 SICP Data abstractions
CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5
Streams Sections 3.5.1,3.5.2 Pages
The Metacircular Evaluator
Lecture 18 Infinite Streams and
FP Foundations, Scheme In Text: Chapter 14.
PPL Sequence Interface.
Lecture 18.
Lecture #8 מבוא מורחב.
6.001 SICP Streams – the lazy way
CSCE 314: Programming Languages Dr. Dylan Shell
Streams, Delayed Evaluation and a Normal Order Interpreter
topics mutable data structures
6.001 SICP Variations on a Scheme
6.001 SICP Data abstractions
Announcements Quiz 5 HW6 due October 23
Lecture #7 מבוא מורחב.
List and list operations (continue).
Today’s topics Abstractions Procedural Data
Lecture # , , , , מבוא מורחב.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
More Scheme CS 331.
Presentation transcript:

Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008

Fall 2008Programming Development Techniques 2 Before we get started… Let me go back to a function we were writing at the end of class and let’s see if I can explain more clearly why I reacted the way I did to the function we wrote and the comments (parameter names) associated with the function.

Fall 2008Programming Development Techniques 3 Background (1 st mistake) We never gave a definition of what a tree (of numbers was) A tree of numbers is –A number –A list whose elements are trees of numbers

Fall 2008Programming Development Techniques 4 ; takes a tree of numbers and returns ; a tree that looks the same but the ; numbers have been scaled by num (define (map-scale-tree tree num) (if (pair? tree) (map (lambda (x) (scale-tree x num)) tree) ; node case (* num tree))) ; leaf case

Fall 2008Programming Development Techniques 5 What was I writing? Notice that when I was leading the writing of the function, I hadn’t bothered to carefully pay attention to the comments (that were on the slide) In the variable naming convention I had used I was taking a tree to be an arbitrarily complex LIST whose atomic elements were numbers. In that case ALL calls to the function should take a LIST (and not a number). This forces a different strategy for writing the function.

Fall 2008Programming Development Techniques 6 ; takes a list whose atomic elements are numbers ; and returns a list that looks the same except ; the original numbers have been scaled by num (define (map-scale-num-list elist num) (map (lambda (x) (cond ((pair? x) (map-scale-num-list x num)) (else (* x num)))) elist)) (map-scale-num-list '((2 (1) (4 3))) 5)  '((10 (5) (20 15)))

Fall 2008Programming Development Techniques 7 Sequences as conventional interfaces A process can often be decomposed into a sequence of stages Examples compiling finding words that are common to two text files

Fall 2008Programming Development Techniques 8 Common types of stages Enumerate – the individual pieces of interest Filter – to isolate the pieces you are interested in Transduce – change the pieces in some way Accumulate – put the changed pieces back together

Fall 2008Programming Development Techniques 9 Sum of squares of odd leaves in a tree 1) Make list of all leaves [enumerate] 2) Extract the odd leaves from list [filter] 3) Make list of the squares [transduce] 4) Sum up the squares [accumulate]

Fall 2008Programming Development Techniques 10 A definition not based on stages ; sums the squares of all odd elements in a tree (define (sum-of-odd-squares tree) (cond ((null? tree) 0) ((not (pair? tree)) (if (odd? tree) (square tree) 0)) (else (+ (sum-of-odd-squares (car tree)) (sum-of-odd-squares (cdr tree))))))

Fall 2008Programming Development Techniques 11 Definition based on stages (define (sum-of-odd-squares tree) (accumulate + 0 (map square (filter odd? (enumerate-tree tree))))) [accumulate][transduce][filter][enumerate]

Fall 2008Programming Development Techniques 12 A general-purpose filter ; returns a list containing those ; elements of lst that return non-#f for ; test (define (filter test lst) (cond ((null? lst) empty) ((test (car lst)) (cons (car lst) (filter test (cdr lst)))) (else (filter test (cdr lst))))) > (filter even? (list )) --> ( )

Fall 2008Programming Development Techniques 13 A general-purpose accumulator ; put together the elements of lst using ; binary-op (define (accumulate binary-op init-val lst) (if (null? lst) init-val (binary-op (car lst) (accumulate binary-op init-val (cdr lst)))))

Fall 2008Programming Development Techniques 14 Accumulate examples (accumulate + 0 (list )) --> 20 (accumulate * 1 (list )) --> 384 (accumulate cons () (list ))  ( ) (accumulate append () ‘((a b) (c d) (e f)))  (a b c d e f)

Fall 2008Programming Development Techniques 15 More Accumulate Examples (accumulate (lambda (x y) (if (< x y) y x)) 0 (list )) --> 8

Fall 2008Programming Development Techniques 16 Enumerators are problem dependent ; makes a list out of elements between ; numbers lo and hi (define (enumerate-interval lo hi) (if (> lo hi) () (cons lo (enumerate-interval (+ lo 1) hi)))) (enumerate-interval 3 10) --> ( )

Fall 2008Programming Development Techniques 17 Enumerating leaves ; enumerates the leaves of a tree (define (enumerate-tree tree) (cond ((null? tree) ()) ((not (pair? tree)) (list tree)) (else (append (enumerate-tree (car tree)) (enumerate-tree (cdr tree)))))) if x --> ((1 3) 2 (5 (4 6))), then (enumerate-tree x) --> ( )

Fall 2008Programming Development Techniques 18 Now we can do it! (define (sum-of-odd-squares tree) (accumulate + 0 (map square (filter odd? (enumerate-tree tree))))) if x --> ((1 3) 2 (5 (4 6))), then (sum-of-odd-squares x) --> 35

Fall 2008Programming Development Techniques 19 Why decompose into stages? Because procedures that are decomposed into stages are easier to understand and to write

Fall 2008Programming Development Techniques 20 Data processing example (define (salary-of-highest-paid-programmer records) (accumulate max 0 (map salary (filter programmer? records))))

Fall 2008Programming Development Techniques 21 Nested mappings maps are like loops, converting one list into another loops can be nested; so can maps Sometimes we want the results of inner lists appended together instead of returned as a list of lists

Fall 2008Programming Development Techniques 22 Example using nested maps Given a positive integer n, find all integer triples such that 1 <= j j < i i <= n i+j is a perfect square (i.e., the square of another integer)

Fall 2008Programming Development Techniques 23 (square-sum-pairs 8)  ((3 1 4) (8 1 9) (7 2 9) (6 3 9) (5 4 9)) Strategy: find possible perfect squares, then find the ways that they can be written as sums of two integers

Fall 2008Programming Development Techniques 24 Decomposition of problem 1) Enumerate numbers from 1 through n ( ) 2) Make list of squares of these numbers ( ) 3) Save only the squares < 2*n [j < i <= n, so i+j <= n + n - 1 = 2n - 1] (1 4 9)

Fall 2008Programming Development Techniques 25 Last stage 4) For each square that was saved, make a list of all the triples such that i+j = the square. Append these lists together rather than returning a list of lists of triples. If n = 8, we want to get the list ((3 1 4) (8 1 9) (7 2 9) (6 3 9) (5 4 9))

Fall 2008Programming Development Techniques 26 Code for first 3 steps 1) Enumerate numbers from 1 through n 2) Make list of squares of these numbers 3) Save only the squares < 2*n (filter (lambda (s) (< s (* 2 n))) (map square (enumerate-interval 1 n)))

Fall 2008Programming Development Techniques 27 How do we do the last stage 4) For each square that was saved, make a list of all the triples such that i+j = the square. Append these lists together rather than returning a list of lists of triples.

Fall 2008Programming Development Techniques 28 One possible solution (define (square-sum-pairs n) (accumulate append empty (map (lambda (s) (map (lambda (j) (list (- s j) j s)) (enumerate-interval (max 1 (- s n)) (* 0.5 ( - s 1)))))

Fall 2008Programming Development Techniques 29 (continued) (filter (lambda (s) (< s (* 2 n))) (map square (enumerate-interval 1 n))))))

Fall 2008Programming Development Techniques 30 A general-purpose procedure (define (flatmap proc list) (accumulate append empty (map proc list)))

Fall 2008Programming Development Techniques 31 Another possible solution (define (square-sum-pairs n) (flatmap (lambda (s) (map (lambda (j) (list (- s j) j s)) (enumerate-interval (max 1 (- s n)) (* 0.5 (- s 1))))) (filter (lambda (s) (< s (* 2 n))) (map square (enumerate-interval 1 n)))))