Download presentation
Presentation is loading. Please wait.
Published byAbraham Hodge Modified over 9 years ago
1
David Evans http://www.cs.virginia.edu/evans CS200: Computer Science University of Virginia Computer Science Lecture 9: Strange Loops and Sinister Repeaters Do be do be do
2
4 February 2004CS 200 Spring 20042 Menu Defining Control Structures –repeat –while –for PS2 PS3
3
4 February 2004CS 200 Spring 20043 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
4 February 2004CS 200 Spring 20044 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)
5
4 February 2004CS 200 Spring 20045 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)))))
6
4 February 2004CS 200 Spring 20046 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)))))
7
4 February 2004CS 200 Spring 20047 (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 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)
8
4 February 2004CS 200 Spring 20048 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)
9
4 February 2004CS 200 Spring 20049 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
10
4 February 2004CS 200 Spring 200410 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!
11
4 February 2004CS 200 Spring 200411 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)
12
4 February 2004CS 200 Spring 200412 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))
13
4 February 2004CS 200 Spring 200413 (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 ).
14
4 February 2004CS 200 Spring 200414 (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))))
15
4 February 2004CS 200 Spring 200415 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))))
16
4 February 2004CS 200 Spring 200416 (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)
17
4 February 2004CS 200 Spring 200417 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))))
18
4 February 2004CS 200 Spring 200418 PS3: Lindenmayer System Fractals
19
4 February 2004CS 200 Spring 200419 L-Systems CommandSequence ::= ( CommandList ) CommandList ::= Command CommandList CommandList ::= Command ::= F Command ::= RAngle Command ::= OCommandSequence
20
4 February 2004CS 200 Spring 200420 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
21
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)
22
4 February 2004CS 200 Spring 200422 Level 2 Level 3
23
4 February 2004CS 200 Spring 200423 The Great Lambda Tree of Ultimate Knowledge and Infinite Power
24
4 February 2004CS 200 Spring 200424 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.