List Recursion

Document Sample
List Recursion Powered By Docstoc
					    Lecture 7:
List Recursion



 CS200: Computer Science
 University of Virginia                   David Evans
 Computer Science          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   2
                  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             3
                     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     4
 Defining Recursive Procedures
1. Be optimistic.
     – Assume you can solve it.
     – If you could, how would you solve a bigger
       problem.
2. Think of the simplest version of the
   problem, something you can already
   solve. (This is the base case.)
3. Combine them to solve the problem.

30 January 2004      CS 200 Spring 2004             5
 Defining Recursive Procedures
                                             on Lists
1. Be optimistic. Be very optimistic
     – Assume you can solve it.
     – If you could, how would you solve a bigger
       problem.        Assume we can solve it for the cdr
2. Think of the simplest version of the
   problem, something you can already
   solve.        The simplest version is usually null
3. Combine them to solve the problem.
                  Combine something on the car of the list with
30 January 2004            CS 200 evaluation on the cdr.
                  the recursive Spring 2004                  6
           Gauss Sum
 (gauss-sum n)
     =1+2+…+n


 (define (gauss-sum n)
    (if (= n 0)
        0
        (+ n (gauss-sum (- n 1)))))

30 January 2004   CS 200 Spring 2004   7
                   Defining Sumlist
                                               > (sumlist (list 1 2 3 4))
                                               10
 (define sumlist                               > (sumlist null)
                                               0
    (lambda (lst)
       (if (null? lst)
                  0
                  ( + (car lst) (sumlist (cdr lst))


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


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

30 January 2004           CS 200 Spring 2004                         10
                  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          11
                          (define insertl
                             (lambda (lst f stopval)
     Definitions                (if (null? lst)
                                    stopval
 (define (sumlist lst)              (f (car lst)
                                       (insertl (cdr lst) f stopval)))))
    (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                            12
                    Charge
• PS2 Due Monday
    – 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              13