240 likes | 252 Vues
Lecture 9: Strange Loops and Sinister Repeaters Do be do be do. David Evans http://www.cs.virginia.edu/evans. CS200: Computer Science University of Virginia Computer Science. Menu. Defining Control Structures repeat while for PS2 PS3. Trick(y) Question. Define a procedure
E N D
Lecture 9: Strange Loops and Sinister Repeaters Do be do be do David Evans http://www.cs.virginia.edu/evans CS200: Computer Science University of Virginia Computer Science
Menu • Defining Control Structures • repeat • while • for • PS2 • PS3 CS 200 Spring 2004
Trick(y) Question Define a procedure (repeat expr n) that evaluates the expression exprn 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. CS 200 Spring 2004
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) CS 200 Spring 2004
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))))) CS 200 Spring 2004
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))))) CS 200 Spring 2004
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) (define (crepeat f n) (if (= n 0) null (cons (f) (crepeat f (- n 1))))) > (crepeat (lambda () 25) 4) (25 25 25 25) > (define (power n m) (apply * (crepeat (lambda () n) m))) > (power 2 10) 1024 CS 200 Spring 2004
Variation Repetition is more interesting if we can do different things each time Define a procedure (define (dofor proc startend) that produces a list of applying proc to all integers between start and end. (dofor square 0 4) (0 1 4 9 16) CS 200 Spring 2004
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 CS 200 Spring 2004
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! CS 200 Spring 2004
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) CS 200 Spring 2004
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)) CS 200 Spring 2004
(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). CS 200 Spring 2004
(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)))) CS 200 Spring 2004
Using for (define (for index stoptest combine next accum) (if (stoptest index) accum (for (next index) stoptest combine next (combine index accum)))) (define (gauss-sum n) (for 1 (lambda (index) (> index n)) + (lambda (x) (+ 1 x)) 0)) CS 200 Spring 2004
> (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 (define (for index stoptest combine next accum) (if (stoptest index) accum (for (next index) stoptest combine next (combine index accum)))) (next index) (define (gauss-sum n) (for 1 (lambda (index) (> index n)) + (lambda (x) (+ 1 x)) 0)) (combine index accum) CS 200 Spring 2004
electorate-voters (define (for index stoptest combine next accum) (if (stoptest index) accum (for (next index) stoptest combine next (combine index accum)))) (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 CS 200 Spring 2004
PS3:Lindenmayer System Fractals CS 200 Spring 2004
L-Systems CommandSequence ::= ( CommandList ) CommandList ::= CommandCommandList CommandList ::= Command ::= F Command ::= RAngle Command ::= OCommandSequence CS 200 Spring 2004
L-System Rewriting CommandSequence ::= ( CommandList ) CommandList ::= CommandCommandList CommandList ::= Command ::= F Command ::= RAngle Command ::= OCommandSequence 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? CS 200 Spring 2004
Level 1 Level 0 (F) Start: (F) F (F O(R30 F) F O(R-60 F) F) (F O(R30 F) F O(R-60 F) F)
Level 2 Level 3 CS 200 Spring 2004
The Great Lambda Tree of Ultimate Knowledge and Infinite Power CS 200 Spring 2004
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 CS 200 Spring 2004