# Class 18: Pairs

Back to Recursion with Natural Numbers. On to Pairs, Continued.

Held Monday, September 30, 2002

Summary

Today we visit some of the behind the scenes structures in Scheme, particularly the pairs that `cons` creates.

Useful Files

Assignments

Notes

• One of you asked me a pair of questions on Friday: Is it true that you're the hardest grader in CS? and If so, does that mean that doing well in your class suggests I'll do well in my other CS classes?
• No, I don't think I'm the hardest grader.
• I do tend to assign some harder problems, though, particularly at the introductory level.
• Not all students who do well in my classes do as well in the upper-level courses.
• Are there any questions on my notes on homework 2?
• No, I haven't graded homework 3.
• Are there any questions on homework 4?
• No, the other CS151 and this class aren't covering topics in the same order.
• Yes, we cover the same core topics.
• But we also look at some different topics.
• I haven't done much rabble rousing this semester, so ...
• The faculty are making motions about the long-range planning committee today. Has student government made any comments on student or staff representation on that committee? It should.
• I know a number of people are concerned about the destruction of Darby. I'll claim that (1) Nothing is inevitible until it's done and (2) There are certainly tactics available for saving Darby (primarily ones that involve raising money or threatening to withhold money).

Overview

• Behind the scences in Scheme: Memory
• Pairs and Cons cells
• Dotted pairs
• Lab

## Behind the Scenes

• In Scheme, every value that you create takes up computer memory.
• Typically, the memory allocated to a value includes the type of the value and the contents of the value.
• As you might guess, different values have their contents arranged differently.
• Scheme also keeps a table (or tables) that associates names with values in memory. (Picture to be drawn in class.)
• Why is this important? Because it can help us think about some things.

## Cons Cells

• Perhaps the most interesting values are the things that `cons` creates. We often call these cons cells or pairs
• The first value of a pair is a reference to the first parameter to cons. The second value is a reference to the second parameter.
• Let's look at some examples. (Again, pictures in class.)
• (cons 3 4)
• (cons a b)
• (cons ('a null))

## Dotted Pairs

• Although we've used lists as the second parts of each cons cell, you can use other values. If the second part of a cons cell is not a list, you get what is called a dotted pair.
• The shorthand for a dotted pair is `(x . y)`.
• You'll also see the dot at the end of a long sequence of cons cells.

## History

Thursday, 29 August 2002 [Samuel A. Rebelsky]

Sunday, 29 September 2002 [Samuel A. Rebelsky]

• Updated slightly.

Back to Recursion with Natural Numbers. On to Pairs, Continued.

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

This document was generated by Siteweaver on Mon Dec 2 08:41:10 2002.
The source to the document was last modified on Sun Sep 29 22:31:11 2002.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Outlines/outline.18.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu