Lecture8

16
Class 8: Recursive List Procedures cs1120 Fall 2011 David Evans 9 September 2011

description

 

Transcript of Lecture8

Page 1: Lecture8

Class 8: Recursive List Procedures

cs1120 Fall 2011David Evans9 September 2011

Page 2: Lecture8

Plan

List ReviewDefining is-list?Quiz CommentsRecursive List Procedures

Page 3: Lecture8

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

Page 4: Lecture8

Is it a List?

(cons null (cons 1 2))

(cons (cons 1 null) (cons 2 null))

Page 5: Lecture8

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.

Page 6: Lecture8

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.

Page 7: Lecture8

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.

Page 8: Lecture8

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

Page 9: Lecture8

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!

Page 10: Lecture8

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!

Page 11: Lecture8

Me: Enough of an optimist (see question 6) and realist that I may decide to have an extra, surprise

quiz some day next week.

Page 12: Lecture8

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!

Page 13: Lecture8

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.

Page 14: Lecture8

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!)

Page 15: Lecture8

Lectures/Group Work

Only G

1 Lecture, 2 Groups

2 Lectures, 1 Group

Mostly Lectures

Only Lectures

0 5 10 15 20 25

Page 16: Lecture8

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.