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
Unformatted text preview: D ATA A BSTRACTION 6 GEORGE WANG email@example.com Department of Electrical Engineering and Computer Sciences University of California, Berkeley June 29, 2010 1 Pairs 1.1 Overview To represent data types that have component parts (like the rank and suit of a card), you have to have some way to aggregate information. Many languages have the idea of an array that groups some number of elements. In Lisp the most basic aggregation unit is the PAIRtwo things combined to form a bigger thing. If you want more than two parts you can hook a bunch of pairs together. Well discuss this more later. The constructor for pairs is cons ; the selectors are car and cdr . CAR CDR PAIR The book uses pairs to represent many different abstract data types: rational numbers (numerator and denominator), complex numbers (real and imaginary parts), points (x and y coordinates), intervals (low and high bounds), and line segments (two endpoints). 1 rational denominator numerator interval high low complex imaginary real point y x segment point2 point1 segment y x y x Notice that in the case of line segments we think of the representation as one pair containing two points, not as three pairs containing four numbers. Thats what it means to respect a data abstraction. 1.2 Exercise: cons Construct the following diagram using cons . 6 4 5 3 2 1.3 Naming So why the heck is it called car and cdr? Well, turns out that a long time ago, one of the earliest machines, the IBM 704, the machine to implement them had a memory which contained something called a address register and a decrement register. Therefore, car stands for the Contents of Address Register, and cdr stands for Contents of Decrement Register. But theres a very useful reason to using things like this, and its used when we are going to combine our pairs into bigger pairs. The important thing is that we can combine car and cdr in a useful way, such that we get something like caddar which is the (car (cdr (cdr (car ...)))) of something. How do you pronounce that out? Well, load ~cs61a/lib/prounounce.scm , and it will tell you. This is the most important part of Scheme! If you want to be a lisp wizard, you have to be able to say it right.important part of Scheme!...
View Full Document