1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
<HTML>
<HEAD>
<TITLE>Simply Scheme Appendix A: Running Scheme</TITLE>
</HEAD>
<BODY>
<CITE>Simply Scheme</CITE>:
<CITE>Introducing Computer Science</CITE> 2/e Copyright (C) 1999 MIT
<H2>Appendix A</H2>
<H1>Running Scheme</H1>
<TABLE width="100%"><TR><TD>
<IMG SRC="../simply.jpg" ALT="cover photo">
<TD><TABLE>
<TR><TD align="right"><CITE><A HREF="http://www.cs.berkeley.edu/~bh/">Brian
Harvey</A><BR>University of California, Berkeley</CITE>
<TR><TD align="right"><CITE><A HREF="http://ccrma.stanford.edu/~matt">Matthew
Wright</A><BR>University of California, Santa Barbara</CITE>
<TR><TD align="right"><BR>
<TR><TD align="right"><A HREF="../pdf/ssch27.pdf">Download PDF version</A>
<TR><TD align="right"><A HREF="../ss-toc2.html">Back to Table of Contents</A>
<TR><TD align="right"><A HREF="../ssch26/preview.html"><STRONG>BACK</STRONG></A>
chapter thread <A HREF="appendix-cl.html"><STRONG>NEXT</STRONG></A>
<TR><TD align="right"><A HREF="http://mitpress.mit.edu/0262082810">MIT
Press web page for <CITE>Simply Scheme</CITE></A>
</TABLE></TABLE>
<HR>
<P>The precise incantations needed to start Scheme depend on the particular
version you're using and the model of computer and operating system you have.
It's beyond the scope of this book to teach you the first steps in using a
computer; we assume you've already used other programs, if not Scheme.
But in this appendix we suggest a few general ideas and point out some
knotty details.
<P>One thing that beginners often forget is that a computer generally has many
different programs available, and each one has its own capabilities and its
own method of operation. If you think of yourself as interacting with "the
computer," you're likely to try to use a command suitable for one program
when you're actually using a different program. In learning to program in
Scheme, you'll probably use at least three programs: Scheme itself, the
operating system's <EM>shell</EM> (which is callled <EM>finder</EM> on the
Macintosh and <EM>explorer</EM> on Windows), and a text editor. (The text
editor may be part of the Scheme package or it may be an entirely separate
program.) The shell allows you to run other programs, such as a printing
utility or an electronic mail reader.
<P>If you say <CODE>(+ 2 3)</CODE> to your text editor, it won't respond by printing
<CODE>5</CODE>. Instead, it will insert the seven characters that you typed into
the file that you're editing. If you type the same thing to Scheme, it will
evaluate the expression.
<P><H2>The Program Development Cycle</H2>
<P>Scheme is an interactive language: You can write a program by typing
its definition directly into the Scheme interpreter. This ability
to interact with Scheme is a great advantage for one-time
calculations and for exploratory work, but it's not the best
approach for the systematic development of a large program.
<P>There are two issues to consider. First, when writing a large program, you
generally don't get it perfect the first time. You make both typing errors
and program logic errors, and so you must be able to revise a definition.
Typing directly to Scheme, the only way to make such a revision is to retype
the entire definition. Second, Scheme does not provide a mechanism to save
your work in a permanent file.
<P>For these reasons, programs are generally typed into another program, a text
editor, rather than directly at the Scheme prompt. As we'll explain in the
next section, there are several ways in which an editing program can be <EM>integrated</EM> with Scheme, so that the work you do in the editor can be
communicated easily to Scheme. But the distinction between Scheme and the
editor is easiest to understand if we start by considering the worst possible
situation, in which the two are not integrated.
<P>Imagine, therefore, that you have two separate programs available on
your computer. One program is a Scheme interpreter. When you start
Scheme, you may see some initial message, and then you see a prompt,
which is a signal from Scheme that it's ready for you to type
something. In this book we've used the character "<CODE>></CODE>" as the
prompt. Then, as we explain in the text, you can type an
expression, and Scheme will compute and print the value:
<P><PRE>> (+ 2 3)
5
</PRE>
<P>Your other program is a text editor. This might be a
general-purpose word processing program, with facilities for
fancy text formatting, or it might be an editor intended
specifically for computer programs. Many editors "know" about
Lisp programs and have helpful features, such as automatic
indentation, selection of complete expressions, and showing you the
matching open parenthesis when you type a close parenthesis.
<P>To write a program, you use the editor. Although you are typing
Scheme expressions, you're not talking to Scheme itself, and so the
expressions are not evaluated as you type them. Instead, they just
appear on the screen like any other text. When you're ready to try
out your program, you tell the editor to save the text in a file.
(The command to save the program text is the same as it would be for
any other text; we assume that you already know how to use the
editor on your computer.) You can give the file any name you want,
although many people like to use names like <CODE><EM>something</EM></CODE><CODE>.scm</CODE> to make it easy to recognize files that contain Scheme
programs.
<P>Now you switch from the editor to Scheme. To read your program file into
Scheme, you enter the expression
<P><PRE>(load "<EM>something</EM>.scm")
</PRE>
<P>This tells Scheme to read expressions from the specified
file.<A NAME="text1" HREF="appendix-running.html#ft1">[1]</A>
<P>Once Scheme has read the program definitions from your file, you
can continue typing expressions to Scheme in order to test your
program. If this testing uncovers an error, you will want to
change some definition. Instead of typing the changed definition
directly into Scheme, which would only make a temporary change in
your program, you switch back to the editor and make the change in
your program file. Then switch back to Scheme, and <CODE>load</CODE> the
corrected file.
<P>This sequence of steps—edit a file, make changes, save the
file, switch to Scheme, load the file, test the program, find an
error—is called a "development cycle" because what comes
after "find an error" is editing the file, beginning another
round of the same steps.
<P><H2>Integrated Editing</H2>
<P>The development process can become much more convenient if Scheme and the
editor "know about" each other. For example, instead of having to reload
an entire file when you change one procedure definition, it's faster if your
editor can tell Scheme just the one new definition. There are three general
approaches to this integration: First, the editor can be in overall charge,
with the Scheme interpreter running under control of the editor. Second,
Scheme can be in charge, with the editor running under Scheme's
supervision. Third, Scheme and the editor can be separate programs, both
running under control of a third program, such as a window system, that
allows information to be transferred between them.
<P>If you're using a Unix system, you will be able to take a
separate editor program and run Scheme from within that editor.
The editor can copy any part of your program into the running
Scheme, as if you had typed it to Scheme yourself. We use Jove,
a free, small, fast version of EMACS. Most people use the more
featureful GNU version of EMACS, which is installed on most Unix
systems and available at <CODE>ftp://prep.ai.mit.edu/pub/gnu/</CODE> and
many mirror sites for download.
<P>If you're using a Macintosh or Windows version of Scheme, it will probably
come with its own text editor and instructions on how to use it. These
editors typically provide standard word-processing features such as cut and
paste, search and replace, and saving files. Also, they typically have a
way to ask Scheme to evaluate an expression directly from the editor.
<P>If you're using SCM under DOS, you should read the section "Editing
Scheme Code" in the <CODE>README</CODE> file that comes with the SCM
distribution. It will explain that editing can be done in different
ways depending on the precise software available to you. You can
buy a DOS editor that works like the Unix editors, or you can ask
SCM to start a separate editor program while SCM remains active.
<P>Finally, if you're running Scheme under Windows or another windowing
operating system (like X or the Macintosh Finder), you can run any editor in
another window and use the cut and paste facility to transfer information
between the editor and Scheme.
<P><H2>Getting Our Programs</H2>
<P>This book uses some programs that we wrote in Scheme. You'll want these
files available to you while reading the book:
<P><TABLE>
<TR><TD><CODE>simply.scm</CODE>
<TD>extended Scheme primitives
<TR><TD><CODE>functions.scm</CODE>
<TD>the <CODE>functions</CODE> program of Chapters 2 and 21
<TR><TD><CODE>ttt.scm</CODE>
<TD>the tic-tac-toe example from Chapter 10
<TR><TD><CODE>match.scm</CODE>
<TD>the pattern matcher example from Chapter 16
<TR><TD><CODE>spread.scm</CODE>
<TD>the spreadsheet program example from Chapter 24
<TR><TD><CODE>database.scm</CODE>
<TD>the beginning of the database project
<TR><TD><CODE>copyleft</CODE>
<TD>the GNU General Public License (see Appendix D)
</TABLE><P>
In particular, the file <CODE>simply.scm</CODE> must be loaded into Scheme to allow
anything in the book to work. Some Scheme systems allow you to load such a
"startup" file permanently, so that it'll be there automatically from then
on. In other versions of Scheme, you must say
<P><PRE>(load "simply.scm")
</PRE>
<P>at the beginning of every Scheme session.
<P>There are three ways to get these program files:
<P><P><TABLE><TR><TH align="right" valign="top">•<TD> <TD valign="top">If you have access to the Internet,
the most recent versions of all these files can be found at
<CODE>ftp://anarres.cs.berkeley.edu/pub/scheme/</CODE>
</TABLE>
<P><TABLE><TR><TH align="right" valign="top">•<TD> <TD valign="top">If you know someone who already has these files, you may copy them
and distribute them freely. (The programs are copyrighted but are provided
under a license that allows unlimited redistribution on a nonprofit
basis; see Appendix D.)
</TABLE><TABLE><TR><TH align="right" valign="top">•<TD> <TD valign="top">If you're stranded on a desert island with nothing but a computer and a
copy of this book, you can type them in yourself; complete listings for all
six programs, plus the GNU Public License, appear in the text of the book.
</TABLE><P>
<P><H2>Tuning Our Programs for Your System</H2>
<P>Almost all of the programs we distribute with this book will work
without modification in the popular versions of Scheme. We've
included "defensive" procedures that allow our programs to work
even in versions that don't conform to current Scheme standards
in various ways. However, there are a few details that we
couldn't make uniform in all versions.
<P>1. Many versions of Scheme include a <CODE>random</CODE> procedure to generate
random numbers, but the standard does not require it, and so we've provided
one just in case. If your Scheme includes a primitive <CODE>random</CODE>, it's
probably better than the one we provide, because we have no way to choose
a different starting value in each Scheme session.
<P>Before loading <CODE>simply.scm</CODE> into Scheme, do the following experiment:
<P><PRE>> (random 5)
</PRE>
<P>If you get an error message, do nothing. If you get a random
number as the result, edit <CODE>simply.scm</CODE> and remove the definition of
<CODE>random</CODE>.
<P>2. Do the following experiment:
<P><PRE>> (error "Your error is" "string")
</PRE>
<P>If the message you get doesn't include quotation marks around the
word <CODE>string</CODE>, then do nothing. But if you do see <CODE>"string"</CODE> with
quotation marks, edit <CODE>simply.scm</CODE> and change the definition of
<CODE>error-printform</CODE> to
<P><PRE>(define (error-printform x) x)
</PRE>
<P>3. Although the Scheme standard says that the <CODE>read</CODE>
procedure should not read the newline character following an
expression that it reads, some old versions of Scheme get this wrong.
<P>After loading <CODE>simply.scm</CODE>, do the following experiment:
<P><PRE>> (read-line)
</PRE>
<P>End the line with the <CODE>return</CODE> or <CODE>enter</CODE> key
(whichever is appropriate in your version of Scheme) as usual,
but don't type a second <CODE>return</CODE> or <CODE>enter</CODE> yet. If
Scheme prints <CODE>()</CODE> right away, skip this paragraph; your
version of Scheme behaves correctly. If, on the other hand,
nothing happens, type another <CODE>return</CODE> or <CODE>enter</CODE>. In
this case you must edit <CODE>functions.scm</CODE> and remove the
invocation of <CODE>read-line</CODE> on the first line of the body of
the <CODE>functions</CODE> procedure.
<P>4. There is a substantial loss of efficiency in treating strings of
digits as numbers in some contexts and as text in other contexts. When
we're treating 1024 as text, we want to be able to take its <CODE>butfirst</CODE>,
which should be 024. But in Scheme, <CODE>024</CODE> is the same as <CODE>24</CODE>, so
instead <CODE>butfirst</CODE> returns a string:
<P><PRE>> (butfirst 1024)
"024"
</PRE>
<P>Yet we want to be able to do arithmetic on this value:
<P><PRE>> (+ 3 (butfirst 1024))
27
</PRE>
<P>To accomplish this, we redefine all of Scheme's arithmetic
procedures to accept strings of digits and convert them to numbers. This
redefinition slows down all arithmetic, not just arithmetic on strange
numbers, and it's only rarely important to the programs we write.
Therefore, we've provided a way to turn this part of the package off and on
again. If your programs run too slowly, try saying
<P><PRE>> (strings-are-numbers #f)
</PRE>
<P>If you find that some program doesn't work because it tries to do
arithmetic on a digit string and gets an error message, you can say
<P><PRE>> (strings-are-numbers #t)
</PRE>
<P>to restore the original behavior of our programs. We recommend
that you leave <CODE>strings-are-numbers</CODE> true while exploring the first few
chapters, so that the behavior of the word data type will be consistent.
When you get to the large example programs, you may want to change to false.
<P><H2>Loading Our Programs</H2>
<P>Scheme's <CODE>load</CODE> procedure doesn't scan your entire disk looking for the
file you want to load. Instead, it only looks in one particular directory
(DOS/Unix) or folder (Macintosh/Windows). If you want to load our programs,
you have to make sure that Scheme can find them.
<P>The first way to accomplish this is to give the full "path" as part of the
argument to <CODE>load</CODE>. Here are some examples:<A NAME="text2" HREF="appendix-running.html#ft2">[2]</A>
<P><PRE>UNIX-SCHEME> (load "/usr/people/matt/scheme-stuff/simply.scm")
WINDOWS-SCHEME> (load "c:\\scheme\\simply.scm")
MAC-SCHEME> (load "Hard Disk:Scheme Folder:simply.scm")
</PRE>
<P>Under Unix, directories in a path are separated by forward slash
characters. Under Windows and DOS, directories are separated by backward slash
characters, which have a special meaning to
Scheme. So you must use double
backslashes as in our example above. On a Macintosh, you separate the parts of
a path with colons. (However, most versions of Scheme for the Macintosh
or Windows
have a load command in one of the menus that opens a standard file
selection dialog box, so you can use that instead.)
<P>The other possibility is to put the files in the place where your version of
Scheme looks for them. In many versions of Scheme, <CODE>load</CODE> looks
for files in the folder that contains the Scheme program itself. Put our
files in that folder.
<P>On Unix, the default loading directory is whatever directory you're in at
the moment. If you want to work on different projects in different
directories, there's no way to make it so that <CODE>load</CODE> will always find
our files. (But see our suggestion about writing <CODE>book-load</CODE>.)
<P><H2>Versions of Scheme</H2>
<P>There are lots of them, both free and commercial. Three places to
look for pointers are
<P><PRE>http://swissnet.ai.mit.edu/scheme-home.html
http://www.schemers.org
http://www.cs.indiana.edu/scheme-repository
</PRE>
<P>In general, there are four things you should be sure to learn about
whatever version of Scheme you choose:
<P><P><TABLE><TR><TH align="right" valign="top">•<TD> <TD valign="top">Most versions of Scheme include a <EM>debugger</EM> to help you find
program errors. If you call a primitive with an argument not in
its domain, for example, Scheme will start the debugger, which will have
features to let you find out where in your program the error occurred.
These debuggers vary greatly among versions of Scheme. The first thing
you should learn is how to <EM>leave</EM> the debugger, so you can get
back to a Scheme prompt!
</TABLE><TABLE><TR><TH align="right" valign="top">•<TD> <TD valign="top">Many versions of Scheme will read an <EM>initialization file</EM> if
you create one. That is, when you start Scheme, it will look for a file
of a particular name (something like <CODE>init.scm</CODE>, but not usually
exactly that), and if there is such a file, Scheme will <CODE>load</CODE> it
automatically. You can copy our <CODE>simply.scm</CODE> file to the proper
filename for your version, and you'll have our added primitives available
every time you start Scheme.
</TABLE><TABLE><TR><TH align="right" valign="top">•<TD> <TD valign="top">Most versions of Scheme provide a <CODE>trace</CODE> capability, but the
format of the trace results are quite different from one version to
another.
</TABLE><TABLE><TR><TH align="right" valign="top">•<TD> <TD valign="top">If you are using a Macintosh, one thing to watch out for is that some
versions of Scheme expect you to use the ENTER key at the end of an
expression, while others expect you to use the RETURN key.
</TABLE>
<P><H2>Scheme Standards</H2>
<P>The Web sites listed above will provide the latest version of the <EM>Revised<SUP><SMALL><I>n</I></SMALL></SUP> Report on the Algorithmic Language Scheme.</EM> You can
get the document in either Postscript or HTML format.
<P>IEEE Standard 1178-1990, <EM>IEEE Standard for the Scheme Programming
Language,</EM> may be ordered from IEEE by calling 1-800-678-IEEE or
908-981-1393 or writing IEEE Service Center, 445 Hoes Lane, P.O. Box 1331,
Piscataway, NJ 08855-1331, and using order number SH14209 ($28 for IEEE
members, $40 for others). ISBN 1-55937-125-0.
<P>
<HR>
<A NAME="ft1" HREF="appendix-running.html#text1">[1]</A> If you see an error message about "end of file" or "EOF," it
probably means that the file you are trying to load contains unbalanced
parentheses; you have started an expression with a left parenthesis, and the
file ended before Scheme saw a matching right parenthesis.<P>
<A NAME="ft2" HREF="appendix-running.html#text2">[2]</A> Suggestion for
instructors: when we teach this class, we define a procedure like
<P><PRE>(define (book-load filename)
(load (string-append "/usr/cs3/progs-from-book/" filename)))
</PRE>
<P>so that students can just say
<P><PRE>(book-load "functions.scm")
</PRE><P>
<P><A HREF="../ss-toc2.html">(back to Table of Contents)</A><P>
<A HREF="../ssch26/preview.html"><STRONG>BACK</STRONG></A>
chapter thread <A HREF="appendix-cl.html"><STRONG>NEXT</STRONG></A>
<P>
<ADDRESS>
<A HREF="../index.html">Brian Harvey</A>,
<CODE>bh@cs.berkeley.edu</CODE>
</ADDRESS>
</BODY>
</HTML>
|