lecture16

lecture16 - T ABLES V ECTORS 16 GEORGE WANG...

This preview shows pages 1–3. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: T ABLES , V ECTORS 16 GEORGE WANG [email protected] Department of Electrical Engineering and Computer Sciences University of California, Berkeley July 20, 2010 1 Tables 1.1 Tables Tables. We’re now ready to understand how to implement the put and get procedures that A&S used at the end of chapter 2. A table is a list of key-value pairs, with an extra element at the front just so that adding the first entry to the table will be no diffferent from adding later entries. (That is, even in an “empty” table we have a pair to set-cdr!) ;;;;; In file cs61a/lectures/3.3/table.scm (define (get key) (let ((record (assoc key (cdr the-table)))) (if (not record) #f (cdr record)))) (define (put key value) (let ((record (assoc key (cdr the-table)))) (if (not record) (set-cdr! the-table (cons (cons key value) (cdr the-table))) (set-cdr! record value))) ’ok) (define the-table (list ’ * table * )) Assoc is in the book: (define (assoc key records) (cond ((null? records) #f) ((equal? key (caar records)) (car records)) 1 (else (assoc key (cdr records))) )) In chapter 2, A&S provided a single, global table, but we can generalize this in the usual way by taking an extra argument for which table to use. That’s how lookup and insert! work. One little detail that always confuses people is why, in creating two-dimensional tables, we don’t need a * table * header on each of the subtables. The point is that lookup and insert! don’t pay any attention to the car of that header pair; all they need is to represent a table by some pair whose cdr points to the actual list of key-value pairs. In a subtable, the key-value pair from the top-level table plays that role. That is, the entire subtable is a value of some key-value pair in the main table. What it means to be "the value of a key-value pair" is to be the cdr of that pair. So we can think of that pair as the header pair for the subtable. 2 Vectors So far we have seen one primitive data aggregation mechanism: the pair. We use linked pairs to represent sequences in the form of lists. The list suffers from one important weakness: Finding the n th element of a list takes time O ( n ) because you have to call cdr n- 1 times. Scheme, like most programming languages, also provides a primitive aggregation mechanism without this weakness. In Scheme it’s called a vector; in many other languages it’s called an array, but it’s the same idea. Accessing the n th element of a vector takes...
View Full Document

{[ snackBarMessage ]}

Page1 / 5

lecture16 - T ABLES V ECTORS 16 GEORGE WANG...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online