Cs1120 Fall 2009 David Evans Lecture 11: Generalizing List Procedures 1.

Slides:



Advertisements
Similar presentations
Unit 3: Probability 3.1: Introduction to Probability
Advertisements

6 Hearts in North North: S: A 9 4 H: A J 10 5 D: A 5 C: K South: S: K 8 H: K Q D: Q C: A 3 IMPs 29 hcpts Bidding: N: 1N S: 2D N: 3H S:
Slide 1 Insert your own content. Slide 2 Insert your own content.
Probability and Conditional Probability. Probability Four balls What is the probability of choosing the ball in the red box? Since there are four balls.
0 - 0.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Combinations Examples
Lecture 18 Dr. MUMTAZ AHMED MTH 161: Introduction To Statistics.
Cs1120 Fall 2009 David Evans Lecture 9: Mostly Not About Matter, Music, and Mayhem Richard Feynmans van Alan Alda playing.
Class 3: Rules of Evaluation David Evans cs1120 Fall 2009.
Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing.
Mock Objects. Unit-testing and TDD are challenging They require some effort to: – Write a test for a small functionality – Refactor production code and.
15.7 Probability Day 3. There are 2 nickels, 3 dimes, and 5 quarters 1.) Find the probability of selecting 1 nickel, 1 dime, and 1 quarter in that order.
Class 6: Programming with Data David Evans cs1120 Fall 2009.
CS 116 Tutorial 6 Strings, Raw Input, Lists. 1. Write a function convert_format that consumes nothing, but takes keyboard input. The program prompts "Enter.
Whiteboardmaths.com © 2004 All rights reserved
Week 1.
Counting Techniques 1. Sequential Counting Principle Section
Index Student Activity 1: Questions to familiarise students with the
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 26: Proving Uncomputability Visualization.
Class 21: Imperative Programming University of Virginia cs1120 David Evans.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 13: Of On and Off Grounds Sorting.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Cs1120 Fall 2009 David Evans Lecture 15: Running Practice.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
Playing Cards Deck of 52 uniquely designed playing cards.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 16: Quicker Sorting.
Refreshing Your Skills for Chapter 10.  If you flip a coin, the probability that it lands with heads up is 1/2.  If you roll a standard die, the probability.
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
David Evans Class 12: Quickest Sorting CS150: Computer Science University of Virginia Computer Science Rose Bush by Jacintha.
David Evans Class 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science.
Compound Probability Pre-AP Geometry. Compound Events are made up of two or more simple events. I. Compound Events may be: A) Independent events - when.
15.3 Counting Methods: Combinations ©2002 by R. Villar All Rights Reserved.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
David Evans Class 20: Quick Sorting CS200: Computer Science University of Virginia Computer Science Queen’s University,
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 12: Something about Sneezewort From.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 10: Puzzling Pegboards.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 9: Strange Loops and Sinister Repeaters.
Algebra II 10.4: Find Probabilities of Disjoint and Overlapping Events HW: HW: p.710 (8 – 38 even) Chapter 10 Test: Thursday.
Review Homework pages Example: Counting the number of heads in 10 coin tosses. 2.2/
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University,
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 5: Recursing on Lists.
Draw 3 cards without replacement from a standard 52 card deck. What is the probability that: 1.They are all red ? 2.At least one is black ? 3.They are.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Of On and Off Grounds Sorting Coffee.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 14: P = NP?
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data.
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 13: Quicksorting CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
The Addition Rule.
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
Lecture 11: All Sorts CS200: Computer Science University of Virginia
David Evans Lecture 9: The Great Lambda Tree of Infinite Knowledge and Ultimate Power CS200: Computer Science University.
Lecture 13: Cost of Sorts CS150: Computer Science
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 10: Quicker Sorting CS150: Computer Science
Strong’s GMAS Review, Week 5, Problems 4 - 5
Lecture 9: The Great Lambda Tree of Knowledge and Power
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
Homework Due Friday.
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

cs1120 Fall 2009 David Evans Lecture 11: Generalizing List Procedures 1

Menu Using list-map PS3 Generalizing List Procedures 2 Ill be away next week (no office hours, but I will have ). Prof. Wes Weimer will lead the classes.

list-map 3 (define (list-map f p) (if (null? p) null (cons (f (car p)) (list-map f (cdr p))))) Equivalent to the built-in procedure map (except map can work on more than one list). From last class:

Maps are Useful From PS2: 4 (define full-deck (list-flatten (list-map (lambda (rank) (list-map (lambda (suit) (make-card rank suit)) (list Hearts Diamonds Clubs Spades))) (list Jack Queen King Ace)))) Image:

Maps are Useful 5 (define (choose-n n lst) ;; operands: a number n and a list (of at least n elements) ;; result: evaluates to a list of all possible was of choosing n elements from lst (if (= n 0) (list null) (if (= (list-length lst) n) (list lst) ; must use all elements (list-append (choose-n n (cdr lst)) ;; all possibilities not using first element (list-map (lambda (clst) (cons (car lst) clst)) (choose-n (- n 1) (cdr lst))))))) From PS2:

Maps are Useful! 6 From PS1: (define (select-mosaic-tiles samples tiles) (map2d find-best-match samples)) (define (map2d f p) (list-map (lambda (inner-list) (list-map f inner-list)) p)) How can we define map2d that maps a list of lists with the input procedure?

PS3: Lindenmayer System Fractals 7 Aristid Lindenmayer ( ) Purple Arrow by Rachel Lathbury and Andrea Yoon Broccoli Fallout by Paul DiOrio, Rachel Phillips

L-Systems CommandSequence ::= ( CommandList ) CommandList ::= Command CommandList CommandList ::= ε Command ::= F Command ::= RAngle Command ::= OCommandSequence 8 Cherry Blossom by Ji Hyun Lee, Wei Wang

L-System Rewriting Start: (F) Rewrite Rule: F (F O(R30 F) F O(R-60 F) F) Work like BNF replacement rules, except replace all instances at once! Why is this a better model for biological systems? CommandSequence ::= ( CommandList ) CommandList ::= Command CommandList CommandList ::= Command ::= F Command ::= RAngle Command ::= OCommandSequence 9

Maps are Useful!! 10 PS3 Question 5: (define (rewrite-lcommands lcommands replacement) (flatten-commands (map ; Procedure to apply to each command lcommands)))

Drawing Functions 11 x y y = (lambda (x) x)

Drawing Arbitrary Curves 12 Rose Bush by Jacintha Henry and Rachel Kay Tie Dye by Bill Ingram (x, y) = (lambda (t) (make-point …))

Curves A curve c is the set of points that results from applying c to every real number t value between 0.0 and How many points? Infinitely many! We can draw an approximate representation of the curve by sampling some of the points.

Defining Curves 14 x y y = (lambda (x) x) (lambda (t) (make-point t t)) (define diag-line (lambda (t) (make-point t t))

Drawing Curves 15 (define (draw-curve-points curve n) (define (draw-curve-worker curve t step) (if (<= t 1.0) (begin (window-draw-point (curve t)) (draw-curve-worker curve (+ t step) step)))) (draw-curve-worker curve 0.0 (/ 1 n))) Does this recursive definition have a base case?

The Great Lambda Tree of Ultimate Knowledge and Infinite Power (Level 5 with color + randomness) 16 PS3 Questions?

Generalizing List Procedures 17 (define (list-map f p) (if (null? p) null (cons (f (car p)) (list-map f (cdr p))))) (define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p))))) (define (list-sum p) (if (null? p) 0 (+ (car p) (list-sum (cdr p))))) (define (is-list? p) (if (null? p) true (if (pair? p) (is-list? (cdr p)) false)))

Similarities and Differences (define (list-map f p) (if (null? p) null (cons (f (car p)) (map f (cdr p))))) (define (list-sum p) (if (null? p) 0 (+ (car p) (sumlist (cdr p))))) (define (list-cruncher ?... ? p) (if (null? p) base result (combiner (car p) (recursive-call... (cdr p))))

list-cruncher (define (list-cruncher baseres carproc combiner p) (if (null? p) baseres (combiner (carproc (car p)) (list-cruncher baseres carproc combiner (cdr p))))) (define (list-sum p) (list-cruncher 0 (lambda (x) x) + p)) (define (list-map f p) (list-cruncher null f cons p))

Can list-cruncher crunch length? (define (list-cruncher baseres carproc combiner p) (if (null? p) baseres (combiner (carproc (car p)) (list-cruncher baseres carproc combiner (cdr p))))) (define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p)))))

Can list-cruncher crunch length? (define (list-cruncher baseres carproc combiner p) (if (null? p) baseres (combiner (carproc (car p)) (list-cruncher baseres carproc combiner (cdr p))))) (define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p))))) (define (list-length p) (list-cruncher 0 (lambda (x) 1) + p))

Can list-cruncher crunch is-list?? (define (list-cruncher baseres carproc combiner p) (if (null? p) baseres (combiner (carproc (car p)) (list-cruncher baseres carproc combiner (cdr p))))) (define (is-list? p) (if (null? p) true (if (pair? p) (is-list? (cdr p)) false))) No! If p is non-null, (car p) is always evaluated by list-cruncher so it produces an error if p is not a list.

Crunchers vs. Accumulators 23 (define (list-cruncher baseres carproc combiner p) (if (null? p) baseres (combiner (carproc (car p)) (list-cruncher baseres carproc combiner (cdr p))))) (define (list-accumulate f base p) (if (null? p) base (f (car p) (list-accumulate f base (cdr p))))) (define (list-sum p) (list-accumulate + 0 p)) (define (list-sum p) (list-cruncher 0 (lambda (x) x) + p))

Crunchers vs. Accumulators 24 (define (list-cruncher baseres carproc combiner p) (if (null? p) baseres (combiner (carproc (car p)) (list-cruncher baseres carproc combiner (cdr p))))) (define (list-accumulate f base p) (if (null? p) base (f (car p) (list-accumulate f base (cdr p))))) Is there any procedure that can be defined using list accumulate that cant be defined using list-cruncher?

Crunchers vs. Accumulators 25 (define (list-cruncher baseres carproc combiner p) (if (null? p) baseres (combiner (carproc (car p)) (list-cruncher baseres carproc combiner (cdr p))))) (define (list-accumulate f base p) (if (null? p) base (f (car p) (list-accumulate f base (cdr p))))) No! Proof-by-construction: (define (list-accumulate f base p) (list-cruncher base (lambda (x) x) f p))

Crunchers vs. Accumulators 26 (define (list-cruncher baseres carproc combiner p) (if (null? p) baseres (combiner (carproc (car p)) (list-cruncher baseres carproc combiner (cdr p))))) (define (list-accumulate f base p) (if (null? p) base (f (car p) (list-accumulate f base (cdr p))))) Gold star bonus: Is there any procedure that can be defined using list cruncher that cant be defined using list accumulate?

Charge You should be able to: – Define, understand, and use recursive procedures on lists – Including: list-map, list-length, list-sum, list-append, list-filter – Understand and define procedures using list-cruncher, list-accumulate, or a similar procedure PS3 due Wednesday Upcoming help hours: – Sunday 6-8:30pm (Olsson 001) – Monday noon-1:30pm (Thorton Stacks) – Monday 4-6:30pm (Small Hall) 27 where the red fern grows by Jessica Geist, Ellen Clarke