1 / 12

David Evans cs.virginia/evans

Lecture 8: Recursion Practice. David Evans http://www.cs.virginia.edu/evans. CS200: Computer Science University of Virginia Computer Science. Simpler Gauss Sum?. (define (insertl lst f stopval) (if (null? lst) stopval (f (car lst)

makara
Download Presentation

David Evans cs.virginia/evans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 8: Recursion Practice David Evans http://www.cs.virginia.edu/evans CS200: Computer Science University of Virginia Computer Science

  2. Simpler Gauss Sum? (define (insertl lst f stopval) (if (null? lst) stopval (f (car lst) (insertl (cdr lst) f stopval)))) > (intsto 5) (1 2 3 4 5) > (intsto 1) (1) Same as previous lecture. (define (gauss-sum n) (insertl (intsto n) + 0)) > (gauss-sum 10) 55 (insertl (list 1 2 3 … n) + 0)  (+ 1 (+ 2 (+ 3 (+ … (+ n 0))))) CS 200 Spring 2004

  3. Reverse intsto ;;; Evaluates to the list (n n-1 … 3 2 1) ;;; if n >= 1, null if n = 0. > (intsfrom 10) (10 9 8 7 6 5 4 3 2 1) (define (intsfrom n) (if (= n 0) null (cons n (intsfrom (- n 1))))) CS 200 Spring 2004

  4. Intsto? (define (intsto n) (if (= n 0) null (list (intsto (- n 1)) n))) > (intsto 5) (((((() 1) 2) 3) 4) 5) CS 200 Spring 2004

  5. append > (append (list 3) (list 4)) (3 4) > (append (list 3) null) (3) > (append null (list 3)) (3) > (append (list 1 2 3) (list 4 5 6)) (1 2 3 4 5 6) Checkup: Try and define append yourself. CS 200 Spring 2004

  6. Intsto (define (intsto n) (if (= n 0) null (append (intsto (- n 1)) (list n)))) > (intsto 5) (1 2 3 4 5) CS 200 Spring 2004

  7. Using Intsto (define (gauss-sum n) (insertl (intsto n) + 0)) (define (factorial n) (insertl (intsto n) * 1)) CS 200 Spring 2004

  8. map (map f lst) Evaluates to the list of values produced by applying f to each element of lst. CS 200 Spring 2004

  9. map (define (map f lst) (if (null? lst) null (cons (f (car lst)) (map f (cdr lst)))) CS 200 Spring 2004

  10. map (define (map f lst) (insertl lst (lambda (a b) (cons (f a) b)) null)) CS 200 Spring 2004

  11. map examples > (map (lambda (x) x) (intsto 5)) (1 2 3 4 5) > (map (lambda (x) (* x x)) (intsto 5)) (1 4 9 16 25) > (map (lambda (row) (display-one-row output-file row tile-width tile-height)) tiles) Displays a photomosaic! CS 200 Spring 2004

  12. Challenge Problem Define a procedure (define (for index test combine 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

More Related