David Evans http://www.cs.virginia.edu/evans Lecture 7: List Recursion CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans
Confusion Is Good! It means you are learning new ways of thinking. 30 January 2004 CS 200 Spring 2004
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
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
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
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
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
Defining Sumlist + (define sumlist (lambda (lst) (if (null? lst) > (sumlist (list 1 2 3 4)) 10 > (sumlist null) (define sumlist (lambda (lst) (if (null? lst) ( (car lst) (sumlist (cdr lst)) + 30 January 2004 CS 200 Spring 2004
Defining Productlist 1 * (define productlist (lambda (lst) > (productlist (list 1 2 3 4)) 24 > (productlist null) 1 (define productlist (lambda (lst) (if (null? lst) ( (car lst) (sumlist (cdr lst)) 1 * 30 January 2004 CS 200 Spring 2004
Defining Length + 1 (define length (lambda (lst) (if (null? lst) > (length (list 1 2 3 4)) 4 > (length null) (define length (lambda (lst) (if (null? lst) ( (car lst) (length (cdr lst)) + 1 30 January 2004 CS 200 Spring 2004
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
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
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