Lecture 9: Strange Loops and Sinister Repeaters Do be do be do CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans.
Download ReportTranscript Lecture 9: Strange Loops and Sinister Repeaters Do be do be do CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans.
Lecture 9: Strange Loops and Sinister Repeaters Do be do be do CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans Menu • Defining Control Structures – repeat – while – for • PS2 • PS3 4 February 2004 CS 200 Spring 2004 2 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 4 February 2004 CS 200 Spring 2004 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. 3 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 2004 CS 200 Spring 2004 4 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 2004 CS 200 Spring 2004 5 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 2004 CS 200 Spring 2004 6 apply takes two parameters: (define (crepeat f n) a procedure (if (= n 0) a list and applies the procedure to null the parameters in the list. (cons (apply * (list 1 2 3)) (f) (* 1 2 3) (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 4 February 2004 CS 200 Spring 2004 7 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) (0 1 4 9 16) 4 February 2004 CS 200 Spring 2004 8 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? 4 February 2004 CS 200 Spring 2004 No 9 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 2004 CS 200 Spring 2004 10 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 2004 CS 200 Spring 2004 11 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)) 4 February 2004 CS 200 Spring 2004 0)) 12 (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 2004 CS 200 Spring 2004 13 (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 2004 CS 200 Spring 2004 14 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)) 4 February 2004 CS 200 Spring 2004 15 (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)) 4 February 2004 > (require (lib "trace.ss")) > (trace +) > (gauss-sum 3) |(+ 1 1) (next index) |2 (combine index accum) |(+ 1 0) |1 |(+ 1 2) |3 |(+ 2 1) |3 |(+ 1 3) |4 |(+ 3 3) |6 6 CS 200 Spring 2004 16 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 4 February 2004 CS 200 Spring 2004 17 PS3: Lindenmayer System Fractals 4 February 2004 CS 200 Spring 2004 18 L-Systems CommandSequence ::= ( CommandList ) CommandList ::= Command CommandList CommandList ::= Command ::= F Command ::= RAngle Command ::= OCommandSequence 4 February 2004 CS 200 Spring 2004 19 L-System Rewriting CommandSequence ::= ( CommandList ) CommandList ::= Command CommandList 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? 4 February 2004 CS 200 Spring 2004 20 Level 0 Level 1 Start: (F) F (F O(R30 F) F O(R-60 F) F) (F) (F O(R30 F) F O(R-60 F) F) Level 2 4 February 2004 Level 3 CS 200 Spring 2004 22 The Great Lambda Tree of Ultimate Knowledge and Infinite Power 4 February 2004 CS 200 Spring 2004 23 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 4 February 2004 CS 200 Spring 2004 24