lecture8
Post on 20-Nov-2014
433 Views
Preview:
DESCRIPTION
TRANSCRIPT
Class 8: Recursive List Procedures
cs1120 Fall 2011David Evans9 September 2011
Plan
List ReviewDefining is-list?Quiz CommentsRecursive List Procedures
3
Recap: Definition of a List
List ::= (cons Element List)List ::= null
A List is either: (1) nullor (2) a Pair where the second part is a List
Is it a List?
(cons null (cons 1 2))
(cons (cons 1 null) (cons 2 null))
Defining is-list?
Define a procedure, is-list?, that takes as input any value and evaluates to true if the input value is a List, and false otherwise.
6
Defining List Procedures
Be very optimistic! Since lists themselves are recursive data structures, most problems involving lists can be solved with recursive procedures.
Think of the simplest version of the problem, something you can already solve. This is the base case. For lists, this is usually when the list is null.
Consider how you would solve the problem using the result for a slightly smaller version of the problem. This is the recursive case. For lists, the smaller version of the problem is usually the cdr of the list.
7
Be very optimistic! Since lists themselves are recursive data structures, most problems involving lists can be solved with recursive procedures.
Think of the simplest version of the problem, something you can already solve. This is the base case. For lists, this is usually when the list is null.
Consider how you would solve the problem using the result for a slightly smaller version of the problem. This is the recursive case. For lists, the smaller version of the problem is usually the cdr of the list.
8
Be very optimistic! …Think of the simplest version of the problem, …Consider how you would solve the problem using the
result for a slightly smaller version of the problem. This is the recursive case. For lists, the smaller version of the problem is usually the cdr of the list.
(define (is-list? p) (if (null? p) true
is-list?
(define (is-list? p) (if (null? p) true (if (pair? p) (is-list? (cdr p)) false)))
A List is either: (1) nullor (2) a Pair where the second part is a List
(define (is-list? p) (or (null? p) (and (pair? p) (is-list? (cdr p)))))
Note: this only works because or and and are special forms. Read the PS1 comments!
Quiz 1 Results: Front Side
6; 8
5; 16
4; 9
3; 9
<3; 6
Anyone doing the reading should have been able to get at least 5 right!
Me: Enough of an optimist (see question 6) and realist that I may decide to have an extra, surprise
quiz some day next week.
Course Pacing
Way too fast
Too fast
A little too fast
Write-in: "fine"
A little too slow
Too slow
0 5 10 15 20 25 30 35
No one from the course came to my office hours on Monday, Tuesday, or Thursday!
Book Exercises
0
5
10
15
20
25
30 Reading a textbook on tough material is not like reading a novel!You will not get it unless you are thinking and trying things yourself.
Solutions are (and have been) currently available through Chapter 4.
I will add more for some problems, but if you get to one that is missing ask me for it. (Or better: send a candidate solution!)
Lectures/Group Work
Only G
1 Lecture, 2 Groups
2 Lectures, 1 Group
Mostly Lectures
Only Lectures
0 5 10 15 20 25
Rest of Today
Group work on defining recursive procedures on lists
It shouldn’t take us ten minutes to get started in Rice like last time!
You don’t need to wait for me to pack up to head over there.
top related