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.

Download Report

Transcript 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.

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
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)
(car pair)
(cdr pair)
30 January 2004
Construct a pair <a, b>
First part of a pair
Second part of a pair
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.
30 January 2004
Combine something on the car of the list with
the recursive
evaluation
on the cdr.
CS 200 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
(define sumlist
(lambda (lst)
(if (null? lst)
> (sumlist (list 1 2 3 4))
10
> (sumlist null)
0
0
( + (car lst) (sumlist (cdr lst))
30 January 2004
CS 200 Spring 2004
8
Defining Productlist
(define productlist
(lambda (lst)
(if (null? lst)
> (productlist (list 1 2 3 4))
24
> (productlist null)
1
1
(*
30 January 2004
(car lst) (sumlist (cdr lst))
CS 200 Spring 2004
9
Defining Length
(define length
(lambda (lst)
(if (null? lst)
> (length (list 1 2 3 4))
4
> (length null)
0
0
( + (car1lst) (length (cdr lst))
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
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 (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