# Class 50: Records

Back to Merge Sort. On to Object Basics.

Held Tuesday, December 3, 2002

Summary

Today we consider how to build records in Scheme. Records group data and permit access to those data by name.

Related Pages

Assignments

Notes

• Are there questions on Homework 10?
• I hope to return graded homeworks to you on Friday.
• Expect exam 3 tomorrow (along with a discussion of your choices for taking the exam).

Overview

## Grouping Data

• As you've seen in a number of examples, it is often convient to group data together into what I typically call an entry.
• You currently know two basic ways to group data: lists and records.
• Lists provide dynamic-sized groups of data but have costly access.
• Arrays provide fixed-sized groups of data with quick access to elements by number.

## Records

• However, as we've seen in our work, it is often more convenient to refer to the parts of a group of data by name.
• For example, given a record for a student, we might ask for the student's first name, last name, or grade.
• Computer scientists use record as the name for a fixed-size group of data with quick access to elements by name.
• Unfortunately, Scheme lacks a built-in record type.

## Implementing Records

• Fortunately, we can implement records on our own.
• The easiest thing to do is to build a vector and treat the elements of the vector as the fields of the record.
• We write procedures to access each part of the vector.
• However, we'd like to be able to identify whether or not each vector is supposed to represent one of our desired objects.
• One clever trick is to build a special value that others find it hard to duplicate and to put it at the front of the vector.
• Consider
```(define produce-type-mark
(let ((type-mark (list 'compound)))
(lambda () type-mark)))
```
• The `let` creates a new symbol table with one entry whose name is `type-mark` and whose value is `(compound)`.
• The `lambda` gets that value.
• Any other list of the symbol `compound` will be `equal?` to the type mark, but not `eq?`.
• Hence, we can use that value.
• Is this ideal? No. Next class, we'll see another way to build things like records.

## History

Thursday, 29 August 2002 [Samuel A. Rebelsky]

Tuesday, 2 December 2002 [Samuel A. Rebelsky]

• Filled in introductory notes.

Back to Merge Sort. On to Object Basics.

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 Dec 3 09:44:58 2002.
The source to the document was last modified on Tue Dec 3 09:35:54 2002.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Outlines/outline.50.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu