# Class 26: Pairs

Back to Simulation. On to Association Lists.

Held: Tuesday, 4 March 2003

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

Related Pages:

Assignments

Notes:

• No class on Friday, 14 March 2003, so that you can leave earlier for break.
• Are there questions on randomness and simulation?
• Are there questions on homework 3?
• Two solutions to the approximate problem.

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.

## Lab

• Any questions?
• Do the lab.
• Be prepared to reflect.

## History

Thursday, 16 January 2003 [Samuel A. Rebelsky]

• First version, created mostly automatically from previous course.

Tuesday, 4 March 2003 [Samuel A. Rebelsky]

• Filled in introductory stuff.

Back to Simulation. On to Association Lists.

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 Tue May 6 09:29:57 2003.
The source to the document was last modified on Tue Mar 4 13:16:46 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003S/Outlines/outline.26.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu