Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7: List Recursion CS200: Computer Science

Similar presentations


Presentation on theme: "Lecture 7: List Recursion CS200: Computer Science"— Presentation transcript:

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

2 Confusion Is Good! It means you are learning new ways of thinking.
30 January 2004 CS 200 Spring 2004

3 Confusing Things Passing procedures as parameters
All the functions you have dealt with since kindergarten only took values as parameters Procedures that produce new procedures All the functions you have dealt with since kindergarten only produced values as results Recursive Procedures Defining things in terms of themselves requires a leap of faith! 30 January 2004 CS 200 Spring 2004

4 Review A list is either: a pair where the second part is a list
or null (note: book uses nil) Pair primitives: (cons a b) Construct a pair <a, b> (car pair) First part of a pair (cdr pair) Second part of a pair 30 January 2004 CS 200 Spring 2004

5 Defining Recursive Procedures
Be optimistic. Assume you can solve it. If you could, how would you solve a bigger problem. Think of the simplest version of the problem, something you can already solve. (This is the base case.) Combine them to solve the problem. 30 January 2004 CS 200 Spring 2004

6 Defining Recursive Procedures
on Lists Be optimistic. Assume you can solve it. If you could, how would you solve a bigger problem. Think of the simplest version of the problem, something you can already solve. Combine them to solve the problem. Be very optimistic Assume we can solve it for the cdr The simplest version is usually null Combine something on the car of the list with the recursive evaluation on the cdr. 30 January 2004 CS 200 Spring 2004

7 Gauss Sum (gauss-sum n) = 1 + 2 + … + n (define (gauss-sum n)
(if (= n 0) (+ n (gauss-sum (- n 1))))) 30 January 2004 CS 200 Spring 2004

8 Defining Sumlist + (define sumlist (lambda (lst) (if (null? lst)
> (sumlist (list )) 10 > (sumlist null) (define sumlist (lambda (lst) (if (null? lst) ( (car lst) (sumlist (cdr lst)) + 30 January 2004 CS 200 Spring 2004

9 Defining Productlist 1 * (define productlist (lambda (lst)
> (productlist (list )) 24 > (productlist null) 1 (define productlist (lambda (lst) (if (null? lst) ( (car lst) (sumlist (cdr lst)) 1 * 30 January 2004 CS 200 Spring 2004

10 Defining Length + 1 (define length (lambda (lst) (if (null? lst)
> (length (list )) 4 > (length null) (define length (lambda (lst) (if (null? lst) ( (car lst) (length (cdr lst)) + 1 30 January 2004 CS 200 Spring 2004

11 Defining insertl (define insertl (lambda (lst f stopval)
(if (null? lst) stopval (f (car lst) (insertl (cdr lst) f stopval))))) 30 January 2004 CS 200 Spring 2004

12 Definitions (define (sumlist lst) (insertl lst + 0))
(define insertl (lambda (lst f stopval) (if (null? lst) stopval (f (car lst) (insertl (cdr lst) f stopval))))) (define (sumlist lst) (insertl lst + 0)) (define (productlist lst) (insertl lst * 1)) (define (length lst) (insertl lst (lambda (head rest) (+ 1 rest)) 0)) 30 January 2004 CS 200 Spring 2004

13 Charge PS2 Due Monday More list recursion practice Monday (& PS3)
Take advantage of remaining lab hours: now – 4:30, Sunday: 3-5:30 Don’t freak out if you can’t get everything! It is possible to get a green star without completing question 5 and 6 (but you should include your attempts and describe what you understand) More list recursion practice Monday (& PS3) 30 January 2004 CS 200 Spring 2004


Download ppt "Lecture 7: List Recursion CS200: Computer Science"

Similar presentations


Ads by Google