CS61A Notes 03 - Lists: Chongyang Wang, Courtesy of Chung Wu and Evan Chou
CS61A Notes 03 - Lists: Chongyang Wang, Courtesy of Chung Wu and Evan Chou
Introducing – the only data structure you’ll ever need in 61A – pairs.
A pair is a data structure that contains two things – the ”things” can be atomic values or even another pair. For
example, you can represent a point as (x . y), and a date as (July . 1). Note the Scheme representation of a
pair; the pair is enclosed in parentheses, and separated by a single period.
These, believe it or not, will be all we’ll ever need to build complex data structures in this course.
The super-cool definition of “list”: a list is either an empty list, or a pair whose car is an element of the list
and whose cdr is another list. Note the recursive definition – a list is a pair that contains a list! So then how does
it end? Wouldn’t there be an infinite number of list? Not so: an empty list, called “nil” and denoted '() is a list
containing no elements. And so it is, that every list ends with the empty list. To test whether a list is empty, you
can use the null? operator on a list.
(2 . (3 . (4 . '() ) ) )
Now, that looks a bit ugly, so Scheme, the nice friendly language that it is, sugar-coats the notation a bit so you get:
(2 3 4)
It’s a bit annoying to write so many cons to define x. So Scheme, the mushy-gushy language that it is, provides an
operator list that takes in elements and returns them in a list. So we can also define x this way:
Note: (car x) is 2, (cdr x) is (3 4), and (car (cdr x)) is 3! Well, it’s a bit tiresome to write (car
(cdr x)) to get the second element of x. So Scheme, again the huggable lovable language that it is, provides a
nifty short hand: (cadr x). This reads cader, and means “take the car of the cdr of”. Similarly, you can use
(caddr x) – caderder – to take the car of the cdr of the cdr of x, which is 4. You can mix and match the ‘a’
and ‘d’ between the ‘c’ and ‘r’ to get the desired element of the list (up to a certain length).
You can also append two lists together. append takes in any number of lists and outputs a list containing those
lists concatenated together. So (append (list 3 4) (list 5 6)) returns (3 4 5 6).
QUESTIONS: Evaluate the following, and draw box-and-pointer diagrams of the results.
_____________________________________________________________________________________________
Don’t You Mean sentence?
Oh stop grumbling. A “sentence” is actually a special kind of “list” – more specifically, a “sentence” is a flat list – a
list without any sublists – whose elements can only be words or numbers. Sentences and lists use different
constructors, selectors, predicates, and higher-order procedures. Using sentence procedures on lists or vice versa
constitutes Data Abstraction Violation, so be very careful which one you're dealing with!
After a week of using sentence procedures, now let's get some practice for the list procedures!
QUESTIONS:
2. Define a procedure length that takes in a list and returns the number of elements within the list.
4. Define a procedure (remove item ls) that takes in a list and returns a new list with item removed
from ls.
5. Define a procedure (interleave ls1 ls2) that takes in two lists and returns one list with elements from
both lists interleaved. So,
(interleave '(a b c d) '(1 2 3 4 5 6 7)) ==> (a 1 b 2 c 3 d 4 5 6 7)