David Evans CS200: Computer Science University of Virginia Computer Science Lecture 9: Strange Loops and Sinister Repeaters.

Slides:



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

David Evans CS150: Computer Science University of Virginia Computer Science Lecture 26: Proving Uncomputability Visualization.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Cs1120 Fall 2009 David Evans Lecture 15: Running Practice.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
David Evans CS200: Computer Science University of Virginia Computer Science Class 27: Modeling Computation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Inheritance.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 16: Quicker Sorting.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
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 CS200: Computer Science University of Virginia Computer Science Lecture 4: Introducing Recursive Definitions.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 11: CS Logo, by Lincoln Hamilton and.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 22: Objectifying Objects.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 4: Recursive Definitions.
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 Lecture 3: Rules of Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 30: Laziness.
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 10: Puzzling Pegboards.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 20: Objects I invented the term Object-
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University,
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: The Value of Everything.
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 23: Programming with Objects.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 5: Recursing on Lists.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Of On and Off Grounds Sorting Coffee.
Cs1120 Fall 2009 David Evans Lecture 18: Changing State Sounds of Colossus:
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
Lecture 3: Rules of Evaluation CS150: Computer Science
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.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 27: Types of Types “It would appear.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 5: Recursion Beware the Lizards!
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr.
(Thunking about Thunks)
Lecture 4: Metacircles Eval Apply David Evans
Lecture 17: Environments CS200: Computer Science
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 13: Quicksorting CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
Lecture 21: Inheritance CS200: Computer Science University of Virginia
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 28: Types of Types
Lecture 13: Cost of Sorts CS150: Computer Science
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 24: Metalinguistics CS200: Computer Science
Lecture 27: In Praise of Idleness CS200: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
Streams, Delayed Evaluation and a Normal Order Interpreter
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 3: Rules of Evaluation CS200: Computer Science
Introduction to the Lab
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 9: Strange Loops and Sinister Repeaters Do be do be do

4 February 2004CS 200 Spring Menu Defining Control Structures –repeat –while –for PS2 PS3

4 February 2004CS 200 Spring Trick(y) Question Define a procedure (repeat expr n) that evaluates the expression expr n times. For example, (repeat (display “Hello”) 3) should produce HelloHelloHello This is impossible! Scheme rule for evaluating applications evaluates all the subexpressions first! So, the repeat procedure never even sees the expression, just the value it evaluates to.

4 February 2004CS 200 Spring Trickery Question Define a procedure (repeat f n) that evaluates (f) n times. If we had repeat, how could we print “Hello” n times? (repeat (lambda () (display "Hello")) 5)

4 February 2004CS 200 Spring Repetition (define (repeat f n) (if (= n 0) (void) (begin (f) (repeat f (- n 1))))) (define (n-times f n) (if (= n 0) (lambda (x) x) (compose f (n-times f (- n 1)))))

4 February 2004CS 200 Spring Accumulation Repetition is more useful if we can collect the results (define (repeat f n) (if (= n 0) (void) (begin (f) (repeat f (- n 1))))) (define (crepeat f n) (if (= n 0) null (cons (f) (crepeat f (- n 1)))))

4 February 2004CS 200 Spring (define (crepeat f n) (if (= n 0) null (cons (f) (crepeat f (- n 1))))) > (crepeat (lambda () 25) 4) ( ) > (define (power n m) (apply * (crepeat (lambda () n) m))) > (power 2 10) 1024 apply takes two parameters: a procedure a list and applies the procedure to the parameters in the list. (apply * (list 1 2 3))  (* 1 2 3)

4 February 2004CS 200 Spring Variation Repetition is more interesting if we can do different things each time Define a procedure (define (dofor proc start end) that produces a list of applying proc to all integers between start and end. (dofor square 0 4)  ( )

4 February 2004CS 200 Spring dofor (define (dofor proc start end) (if (> start end) null (cons (proc start) (dofor proc (+ start 1) end)))) Can you define intsto using dofor ? (define (intsto n) (dofor (lambda (x) x) 1 n)) Can you define map using dofor ? No

4 February 2004CS 200 Spring Generalizing dofor What if we sometimes want to count by 2 ? What if we sometimes want to count by doubling? What if we sometimes want to count by -1 and stop when start <= end ? Make the stopping test and incrementing function procedure parameters!

4 February 2004CS 200 Spring dountil (define (dountil proc stoptest next index) (if (stoptest index) null (cons (proc index) (dountil proc stoptest next (next index))))) > (dountil (lambda (x) x) (lambda (val) (< val 1)) (lambda (x) (- x 1)) 10)

4 February 2004CS 200 Spring Challenge Problem (from last time) Define a procedure (define (for index stoptest combiner next accum) …) that can be used like this: (define (gauss-sum n) (for 1 (lambda (index) (> index n)) + (lambda (x) (+ 1 x)) 0))

4 February 2004CS 200 Spring (define (dountil proc stoptest next index) (if (stoptest index) null (cons (proc index) (dountil proc stoptest next (next index))))) (define (for index stoptest combine next accum) Instead of making a list of all the values, we want to accumulate results in one value ( accum ).

4 February 2004CS 200 Spring (define (dountil proc stoptest next index) (if (stoptest index) null (cons (proc index) (dountil proc stoptest next (next index))))) (define (for index stoptest combine next accum) (if (stoptest index) accum (for (next index) stoptest combine next (combine index accum))))

4 February 2004CS 200 Spring Using for (define (gauss-sum n) (for 1 (lambda (index) (> index n)) + (lambda (x) (+ 1 x)) 0)) (define (for index stoptest combine next accum) (if (stoptest index) accum (for (next index) stoptest combine next (combine index accum))))

4 February 2004CS 200 Spring (define (gauss-sum n) (for 1 (lambda (index) (> index n)) + (lambda (x) (+ 1 x)) 0)) (define (for index stoptest combine next accum) (if (stoptest index) accum (for (next index) stoptest combine next (combine index accum)))) > (require (lib "trace.ss")) > (trace +) > (gauss-sum 3) |(+ 1 1) |2 |(+ 1 0) |1 |(+ 1 2) |3 |(+ 2 1) |3 |(+ 1 3) |4 |(+ 3 3) |6 6 (next index) (combine index accum)

4 February 2004CS 200 Spring electorate-voters (define (electorate-voters electorate start-position end-position) (for start-position (lambda (pos) (> pos end-position)) ;; stop when pos > end-position (lambda (pos accum) (+ (electorate pos) accum)) (lambda (pos) (+ pos (/ 1 electorate-steps))) 0)) ;; initially, accumulator is 0 (define (for index stoptest combine next accum) (if (stoptest index) accum (for (next index) stoptest combine next (combine index accum))))

4 February 2004CS 200 Spring PS3: Lindenmayer System Fractals

4 February 2004CS 200 Spring L-Systems CommandSequence ::= ( CommandList ) CommandList ::= Command CommandList CommandList ::= Command ::= F Command ::= RAngle Command ::= OCommandSequence

4 February 2004CS 200 Spring 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

Level 0 (F) Level 1 F  (F O(R30 F) F O(R-60 F) F) Start: (F) (F O(R30 F) F O(R-60 F) F)

4 February 2004CS 200 Spring Level 2 Level 3

4 February 2004CS 200 Spring The Great Lambda Tree of Ultimate Knowledge and Infinite Power

4 February 2004CS 200 Spring Charge No class Friday, no office hours tomorrow Get started on PS3, it is longer than PS2 Make some interesting fractals –Once you have it working, its easy to produce lots of interesting pictures –Make a better course logo