# Class 20: Pause for Breath

Back to Association Lists. On to Preconditions and Postconditions.

Held Friday, February 23, 2001

Summary

Today we pause to reflect on some of the more difficult problems we've worked on recently and to consider the examination.

Assignments

Due

Notes

• I'll spend a few minutes going over some of the stranger problems from recent labs. Let me know if you have questions.
• There have been enough questions on the "count pairs" problem that I'll go over the solution en masse.
• If we run out of time, I will not postpone today's lab until Monday; I will expect you to do it on your own.
• Yes, I screwed up the output on the extra credit problem on the list recursion lab. You can take the weekend to redo it if you want.

Overview

• Counting cons cells
• Returning more than one kind of value from a recursive procedure
• Exam discussion

## Counting Cons Cells

• Sorry, this won't go in the outline because the problem will remain in the lab for future years.

## Returning Different Kinds of Values

• A number of you have noted that you find it inappropriate to return only one kind of value from a recursive procedure.
• For example, the built-in `assoc` procedure can return an entry from the association list or `#f`.
• Some of you have said that you'd like the `assoc-all` procedure (that returns a list of all the matching entries) to return `#f` when there are no matching entires.
• What can you do about problems like this?
• Write more-complicated recrusive code.
• Change the way you think so that returning the same type for the base case makes sense.
• In the case of `assoc-all`, in which you're supposed to return a list of all the matching entries, it seems natural to me to return the empty list when nothing matches, since that list contains all the matching entries.
• But if you don't like that, your procedure can be something like the following
```(define assoc-all
(lambda (key database)
; Nothing can be in the empty database, so return false.
(if (null? database)
#f
; If we match the car of the database, look in the rest.
(if (equal? key (caar database)
; If the key does not appear any more in the database
; make a list of just the one value we've seen
(if (not (assoc-all key (cdr database)))
(list (car database))
; Otherwise, attach the matched entry to
; the remaining matched entries
(cons (car database) (assoc-all key (cdr database))))
; We didn't match the car, so just look in the rest
(assoc-all key (cdr database)))))))
```
• Ugly, isn't it?

## Exam Distributed

• Look, it's exam 1!
• Policies: Don't cheat.
• Problems: Lots of fun.
• Good luck!

## History

Friday, 12 January 2001

• Created generic outline format for class. Basic details filled in automatically.

Thursday, 22 February 2001

• Added some notes to the body.

Saturday, 24 February 2001

• Changed the topic (which had been on preconditions and postconditions)
• Removed the materials relevant to the old topic
• Wrote everything else new again

Back to Association Lists. On to Preconditions and Postconditions.

Disclaimer: I usually create these pages on the fly. This means that they are rarely proofread and may contain bad grammar and incorrect details. It also means that I may update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This page was generated by Siteweaver on Wed May 5 12:14:56 2004.
This page may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/outline.20.html`.