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