|
Suppose we're using Scheme to model an ice cream shop. We'll certainly need to know all the flavors that are available:
(vanilla ginger strawberry lychee raspberry mocha)
For example, here's a procedure that models the behavior of the salesperson when you place an order:
(define (order flavor) (if (member? flavor '(vanilla ginger strawberry lychee raspberry mocha)) '(coming right up!) (se '(sorry we have no) flavor)))
But what happens if we want to sell a flavor like "root beer fudge ripple" or "ultra chocolate"? We can't just put those words into a sentence of flavors, or our program will think that each word is a separate flavor. Beer ice cream doesn't sound very appealing.
What we need is a way to express a collection of items, each of which is itself a collection, like this:
(vanilla (ultra chocolate) (heath bar crunch) ginger (cherry garcia))
This is meant to represent five flavors, two of which are named by single words, and the other three of which are named by sentences.
Luckily for us, Scheme provides exactly this capability. The data structure
we're using in this example is called a list. The difference
between a sentence and a list is that the elements of a sentence must be
words, whereas the elements of a list can be anything at all: words, #t
, procedures, or other lists. (A list that's an element of another list
is called a sublist. We'll use the name structured
list for a list that includes sublists.)
Another way to think about the difference between sentences and lists is that the definition of "list" is self-referential, because a list can include lists as elements. The definition of "sentence" is not self-referential, because the elements of a sentence must be words. We'll see that the self-referential nature of recursive procedures is vitally important in coping with lists.
Another example in which lists could be helpful is the pattern matcher. We
used sentences to hold known-values
databases, such as this one:
(FRONT YOUR MOTHER ! BACK SHOULD KNOW !)
This would be both easier for you to read and easier for programs to manipulate if we used list structure to indicate the grouping instead of exclamation points:
((FRONT (YOUR MOTHER)) (BACK (SHOULD KNOW)))
We remarked when we introduced sentences that they're a feature we added to Scheme just for the sake of this book. Lists, by contrast, are at the core of what Lisp has been about from its beginning. (In fact the name "Lisp" stands for "LISt Processing.")
When we introduced words and sentences we had to provide ways to take them
apart, such as first
, and ways to put them together, such as sentence
. Now we'll tell you about the selectors and
constructors for lists.
The function to select the first element of a list is called
car
.[1] The function to select the
portion of a list containing all but the first element is called
cdr
, which is pronounced "could-er." These are analogous to first
and butfirst
for words and sentences.
Of course, we can't extract pieces of a list that's empty, so we need a
predicate that will check for an empty list. It's called null?
and it
returns #t
for the empty list, #f
for anything else. This is
the list equivalent of empty?
for words and sentences.
There are two constructors for lists. The function list
takes
any number of arguments and returns a list with those arguments as its
elements.
> (list (+ 2 3) 'squash (= 2 2) (list 4 5) remainder 'zucchini) (5 SQUASH #T (4 5) #<PROCEDURE> ZUCCHINI)
The other constructor, cons
, is used when you already have
a list and you want to add one new element. Cons
takes two arguments,
an element and a list (in that order), and returns a new list whose car
is the first argument and whose cdr
is the second.
> (cons 'for '(no one)) (FOR NO ONE) > (cons 'julia '()) (JULIA)
There is also a function that combines the elements of two or more lists into a larger list:
> (append '(get back) '(the word)) (GET BACK THE WORD)
It's important that you understand how list
, cons
,
and append
differ from each other:
> (list '(i am) '(the walrus)) ((I AM) (THE WALRUS)) > (cons '(i am) '(the walrus)) ((I AM) THE WALRUS) > (append '(i am) '(the walrus)) (I AM THE WALRUS)
When list
is invoked with two arguments, it considers them to be two
proposed elements for a new two-element list. List
doesn't care
whether the arguments are themselves lists, words, or anything else; it just
creates a new list whose elements are the arguments. In this case, it ends
up with a list of two lists.
Cons
requires that its second argument be a list.[2] Cons
will extend that list to form a new list, one element
longer than the original; the first element of the resulting list comes from
the first argument to cons
. In other words, when you pass cons
two arguments, you get back a list whose car
is the first argument to
cons
and whose cdr
is the second argument.
Thus, in this example, the three elements of the returned list consist
of the first argument as one single element, followed by the elements
of the second argument (in this case, two words). (You may be wondering
why anyone would want to use such a strange constructor instead of list
. The answer has to do with recursive procedures, but hang on for a few
paragraphs and we'll show you an example, which will help more than any
explanation we could give in English.)
Finally, append
of two arguments uses the elements of both
arguments as elements of its return value.
Pictorially, list
creates a list whose elements are the arguments:
Cons
creates an extension of its second argument with
one new element:
Append
creates a list whose elements are the elements
of the arguments, which must be lists:
(define (praise flavors) (if (null? flavors) '() (cons (se (car flavors) '(is delicious)) (praise (cdr flavors))))) > (praise '(ginger (ultra chocolate) lychee (rum raisin))) ((GINGER IS DELICIOUS) (ULTRA CHOCOLATE IS DELICIOUS) (LYCHEE IS DELICIOUS) (RUM RAISIN IS DELICIOUS))
In this example our result is a list of sentences. That is,
the result is a list that includes smaller lists as elements, but each of
these smaller lists is a sentence, in which only words are allowed. That's
why we used the constructor cons
for the overall list, but se
for each sentence within the list.
This is the example worth a thousand words that we promised, to show why cons
is useful. List
wouldn't work in this situation. You can
use list
only when you know exactly how many elements will be in your
complete list. Here, we are writing a procedure that works for any number of
elements, so we recursively build up the list, one element at a time.
In the following example we take advantage of structured lists to produce a translation dictionary. The entire dictionary is a list; each element of the dictionary, a single translation, is a two-element list; and in some cases a translation may involve a phrase rather than a single word, so we can get three deep in lists.
(define (translate wd) (lookup wd '((window fenetre) (book livre) (computer ordinateur) (house maison) (closed ferme) (pate pate) (liver foie) (faith foi) (weekend (fin de semaine)) ((practical joke) attrape) (pal copain)))) (define (lookup wd dictionary) (cond ((null? dictionary) '(parlez-vous anglais?)) ((equal? wd (car (car dictionary))) (car (cdr (car dictionary)))) (else (lookup wd (cdr dictionary))))) > (translate 'computer) ORDINATEUR > (translate '(practical joke)) ATTRAPE > (translate 'recursion) (PARLEZ-VOUS ANGLAIS?)
By the way, this example will help us explain why those ridiculous names
car
and cdr
haven't died out. In this not-so-hard program we
find ourselves saying
(car (cdr (car dictionary)))
to refer to the French part of the first translation in the
dictionary. Let's go through that slowly. (Car dictionary)
gives us
the first element of the dictionary, one English-French pairing. Cdr
of that first element is a one-element list, that is, all but the English word
that's the first element of the pairing. What we want isn't the one-element
list but rather its only element, the French word, which is its car
.
This car
of cdr
of car
business is pretty lengthy and
awkward. But Scheme gives us a way to say it succinctly:
(cadar dictionary)
In general, we're allowed to use names like cddadr
up to
four deep in A
s and D
s. That one means
(cdr (cdr (car (cdr something))))
or in other words, take the cdr
of the cdr
of the car
of the cdr
of its argument. Notice that the order of letters
A
and D
follows the order in which you'd write the procedure
names, but (as always) the procedure that's invoked first is the one on
the right. Don't make the mistake of reading cadr
as meaning
"first take the car
and then take the cdr
." It means "take
the car
of the cdr
."
The most commonly used of these abbreviations are cadr
, which selects
the second element of a list; caddr
, which selects the third element;
and cadddr
, which selects the fourth.
You've probably noticed that it's hard to distinguish between a sentence (which must be made up of words) and a list that happens to have words as its elements.
The fact is, sentences are lists. You could take car
of a
sentence, for example, and it'd work fine. Sentences are an
abstract data type represented by lists. We created the sentence
ADT by writing special selectors and constructors that provide a
different way of using the same underlying machinery—a different
interface, a different metaphor, a different point of view.
How does our sentence point of view differ from the built-in Scheme point of view using lists? There are three differences:
• | A sentence can contain only words, not sublists. |
---|
• | Sentence selectors are symmetrical front-to-back. |
---|
• | Sentences and words have the same selectors. |
---|
All of these differences fit a common theme: Words and sentences are meant to represent English text. The three differences reflect three characteristics of English text: First, text is made of sequences of words, not complicated structures with sublists. Second, in manipulating text (for example, finding the plural of a noun) we need to look at the end of a word or sentence as often as at the beginning. Third, since words and sentences work together so closely, it makes sense to use the same tools with both. By contrast, from Scheme's ordinary point of view, an English sentence is just one particular case of a much more general data structure, whereas a symbol[3] is something entirely different.
The constructors and selectors for sentences reflect these three
differences. For example, it so happens that Scheme represents lists in a
way that makes it easy to find the first element, but harder to find the
last one. That's reflected in the fact that there are no primitive
selectors for lists equivalent to last
and butlast
for
sentences. But we want last
and butlast
to be a part of the
sentence package, so we have to write them in terms of the "real" Scheme
list selectors. (In the versions presented here, we are ignoring the issue
of applying the selectors to words.)
(define (first sent) ;;; just for sentences (car sent)) (define (last sent) (if (null? (cdr sent)) (car sent) (last (cdr sent)))) (define (butfirst sent) (cdr sent)) (define (butlast sent) (if (null? (cdr sent)) '() (cons (car sent) (butlast (cdr sent)))))
If you look "behind the curtain" at the implementation, last
is a lot more complicated than first
. But from the point of
view of a sentence user, they're equally simple.
In Chapter 16 we used the pattern matcher's known-values database to
introduce the idea of abstract data types. In that example, the most
important contribution of the ADT was to isolate the details of the
implementation, so that the higher-level procedures could invoke lookup
and add
without the clutter of looking for exclamation
points. We did hint, though, that the ADT represents a shift in how the
programmer thinks about the sentences that are used to represent databases;
we don't take the acronym of a database, even though the database is
a sentence and so it would be possible to apply the acronym
procedure
to it. Now, in thinking about sentences, this idea of shift in viewpoint is
more central. Although sentences are represented as lists, they behave much
like words, which are represented quite differently.[4] Our sentence mechanism highlights the uses of
sentences, rather than the implementation.
The higher-order functions that we've used until now work only for
words and sentences. But the idea of higher-order functions applies
perfectly well to structured lists. The official list versions of every
, keep
, and accumulate
are called map
, filter
,
and reduce
.
Map
takes two arguments, a function and a list, and returns a list
containing the result of applying the function to each element of the list.
> (map square '(9 8 7 6)) (81 64 49 36) > (map (lambda (x) (se x x)) '(rocky raccoon)) ((ROCKY ROCKY) (RACCOON RACCOON)) > (every (lambda (x) (se x x)) '(rocky raccoon)) (ROCKY ROCKY RACCOON RACCOON) > (map car '((john lennon) (paul mccartney) (george harrison) (ringo starr))) (JOHN PAUL GEORGE RINGO) > (map even? '(9 8 7 6)) (#F #T #F #T) > (map (lambda (x) (word x x)) 'rain) ERROR - INVALID ARGUMENT TO MAP: RAIN
The word "map" may seem strange for this function, but it comes
from the mathematical study of functions, in which they talk about a mapping of the domain into the range. In this terminology, one talks
about "mapping a function over a set" (a set of argument values, that is),
and Lispians have taken over the same vocabulary, except that we talk about
mapping over lists instead of mapping over sets. In any case, map
is
a genuine Scheme primitive, so it's the official grownup way to talk about
an every
-like higher-order function, and you'd better learn to like it.
Filter
also takes a function and a list as arguments; it returns a
list containing only those elements of the argument list for which the
function returns a true value. This is the same as keep
, except that
the elements of the argument list may be sublists, and their structure is
preserved in the result.
> (filter (lambda (flavor) (member? 'swirl flavor)) '((rum raisin) (root beer swirl) (rocky road) (fudge swirl))) ((ROOT BEER SWIRL) (FUDGE SWIRL)) > (filter word? '((ultra chocolate) ginger lychee (raspberry sherbet))) (GINGER LYCHEE)
> (filter (lambda (nums) (= (car nums) (cadr nums))) '((2 3) (4 4) (5 6) (7 8) (9 9))) ((4 4) (9 9))
Filter
probably makes sense to you as a name; the metaphor
of the air filter that allows air through but doesn't allow dirt, and so on,
evokes something that passes some data and blocks other data. The only
problem with the name is that it doesn't tell you whether the elements for
which the predicate function returns #t
are filtered in or filtered
out. But you're already used to keep
, and filter
works
the same way. Filter
is not a standard Scheme primitive, but it's a
universal convention; everyone defines it the same way we do.
Reduce
is just like accumulate
except that it works only on
lists, not on words. Neither is a built-in Scheme primitive; both names are
seen in the literature. (The name "reduce" is official in the languages
APL and Common Lisp, which do include this higher-order function as a primitive.)
> (reduce * '(4 5 6)) 120 > (reduce (lambda (list1 list2) (list (+ (car list1) (car list2)) (+ (cadr list1) (cadr list2)))) '((1 2) (30 40) (500 600))) (531 642)
The list?
predicate returns #t
if its argument is a list, #f
otherwise.
The predicate equal?
, which we've discussed earlier as applied to
words and sentences, also works for structured lists.
The predicate member?
, which we used in one of the
examples above, isn't a true Scheme primitive, but part of the word and
sentence package. (You can tell because it "takes apart" a word to look
at its letters separately, something that Scheme doesn't ordinarily do.)
Scheme does have a member
primitive without the question mark that's
like member?
except for two differences: Its second argument must be
a list (but can be a structured list); and instead of returning #t
it
returns the portion of the argument list starting with the element equal to
the first argument. This will be clearer with an example:
> (member 'd '(a b c d e f g)) (D E F G) > (member 'h '(a b c d e f g)) #F
This is the main example in Scheme of the semipredicate
idea that we mentioned earlier in passing. It doesn't have a question mark
in its name because it returns values other than #t
and #f
,
but it works as a predicate because any non-#f
value is considered
true.
The only word-and-sentence functions that we haven't already mentioned are
item
and count
. The list equivalent of item
is called
(short for "reference"); it's different in that it
counts items from zero instead of from one and takes its arguments in the
other order:
list-ref
> (list-ref '(happiness is a warm gun) 3) WARM
The list equivalent of count
is called length
, and
it's exactly the same except that it doesn't work on words.
An example earlier in this chapter was about translating from English to
French. This involved searching for an entry in a list by comparing the
first element of each entry with the information we were looking for. A
list of names and corresponding values is called an association
list, or an a-list. The Scheme primitive assoc
looks up a
name in an a-list:
> (assoc 'george '((john lennon) (paul mccartney) (george harrison) (ringo starr))) (GEORGE HARRISON) > (assoc 'x '((i 1) (v 5) (x 10) (l 50) (c 100) (d 500) (m 1000))) (X 10) > (assoc 'ringo '((mick jagger) (keith richards) (brian jones) (charlie watts) (bill wyman))) #F
(define dictionary '((window fenetre) (book livre) (computer ordinateur) (house maison) (closed ferme) (pate pate) (liver foie) (faith foi) (weekend (fin de semaine)) ((practical joke) attrape) (pal copain))) (define (translate wd) (let ((record (assoc wd dictionary))) (if record (cadr record) '(parlez-vous anglais?))))
Assoc
returns #f
if it can't find the entry you're
looking for in your association list. Our translate
procedure
checks for that possibility before using cadr
to extract the French
translation, which is the second element of an entry.
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Python: module test.tc_displayable</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head><body bgcolor="#f0f0f8">
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#7799ee">
<td valign=bottom> <br>
<font color="#ffffff" face="helvetica, arial"> <br><big><big><strong><a href="test.html"><font color="#ffffff">test</font></a>.tc_displayable</strong></big></big></font></td
><td align=right valign=bottom
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/home/hut/work/ranger/test/tc_displayable.py">/home/hut/work/ranger/test/tc_displayable.py</a></font></td></tr></table>
<p></p>
<p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#aa55cc">
<td colspan=3 valign=bottom> <br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Modules</strong></big></font></td></tr>
<tr><td bgcolor="#aa55cc"><tt> </tt></td><td> </td>
<td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="curses.html">curses</a><br>
</td><td width="25%" valign=top><a href="unittest.html">unittest</a><br>
</td><td width="25%" valign=top></td><td width="25%" valign=top></td></tr></table></td></tr></table><p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ee77aa">
<td colspan=3 valign=bottom> <br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
<tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td>
<td width="100%"><dl>
<dt><font face="helvetica, arial"><a href="unittest.html#TestCase">unittest.TestCase</a>(<a href="builtins.html#object">builtins.object</a>)
</font></dt><dd>
<dl>
<dt><font face="helvetica, arial"><a href="test.tc_displayable.html#TestDisplayable">TestDisplayable</a>
</font></dt><dt><font face="helvetica, arial"><a href="test.tc_displayable.html#TestDisplayableContainer">TestDisplayableContainer</a>
</font></dt></dl>
</dd>
</dl>
<p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
<td colspan=3 valign=bottom> <br>
<font color="#000000" face="helvetica, arial"><a name="TestDisplayable">class <strong>TestDisplayable</strong></a>(<a href="unittest.html#TestCase">unittest.TestCase</a>)</font></td></tr>
<tr><td bgcolor="#ffc8d8"><tt> </tt></