forked from ds26gte/tyscheme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.tex
214 lines (167 loc) · 4.98 KB
/
hello.tex
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
\chapter{Enter Scheme}
\label{hello}
\index{begin@\q{begin}}
\index{display@\q{display}}
\index{newline@\q{newline}}
The canonical first program is the one that says
\q{"Hello, World!"} on the console. Using your
favorite editor, create a file called
\p{hello.scm} with the following contents:
\q{
;The first program
(begin
(display "Hello, World!")
(newline))
}
\n
\index{comment}
The first line is a comment. When Scheme sees a
semicolon, it ignores it and all the following text on
the line.
\index{form}
\index{subform}
\index{console}
\index{standard output}
The \q{begin}-{\em form} is Scheme’s way of
introducing a sequence of {\em subforms}. In this
case there are two subforms. The first is a call to
the
\q{display} procedure that outputs its argument
(the string \q{"Hello, World!"}) to the console (or
“standard output”). It is followed by a \q{newline}
procedure call, which outputs a carriage return.
\index{command line}
\index{MzScheme}
To run this program, first start your Scheme. This is
usually done by typing the name of your Scheme
executable at the operating-system command line.
E.g., in the case of MzScheme \cite{mzscheme},
you type
\p{
mzscheme
}
\n at the operating-system prompt.
\index{listener}
\index{read-eval-print loop}
\index{evaluation}
This invokes the Scheme {\em listener}, which {\em read}s
your input, {\em eval}uates it, {\em print}s the result (if
any), and then waits for more input from you. For this
reason, it is often called the {\em read-eval-print loop}.
Note that this is not much different from your
operating-system command line, which also reads your
commands, executes them, and then waits for more. Like the
operating system, the Scheme listener has its own prompt —
usually this is
\p{>}, but could be something else.
\index{load@\q{load}}
At the listener prompt, {\em load} the file
\p{hello.scm}. This is done by typing
\q{
(load "hello.scm")
}
\n Scheme will now execute the contents of
\p{hello.scm}, outputting \p{Hello, World!} followed
by a carriage return. After this, you will get the listener
prompt again, waiting for more input from you.
Since you have such an eager listener, you need not
always write your programs in a file and load them.
Sometimes, it is easier, especially when you are in an
exploring mood, to simply type expressions directly at
the listener prompt and see what happens. For example,
typing the form
\q{
(begin (display "Hello, World!")
(newline))
}
\n at the Scheme prompt produces
\p{
Hello, World!
}
Actually, you could simply have typed the form
\q{"Hello, World!"} at the listener, and you would have
obtained as result the string
\q{
"Hello, World!"
}
\n because that is the result of the listener evaluating
\q{"Hello, World!"}.
Other than the fact that the second approach produces a
result with double-quotes around it, there is one other
significant difference between the last two programs.
The first (i.e., the one with the \q{begin}) does not evaluate
to anything — the \p{Hello, World!} it emits is a
{\em side-effect} produced by the \q{display} and
\q{newline} procedures writing to the standard output.
In the second program, the form
\q{"Hello, World!"} {\em evaluates} to the result, which
in this case is the same string as the form.
Henceforth, we will use the notation \q{|evalsto} to denote
evaluation. Thus
\q{
E |evalsto v
}
\n indicates that the form \q{E} evaluates to a result value
of \q{v}. E.g.,
\q{
(begin
(display "Hello, World!")
(newline))
|evalsto
}
\n (i.e., nothing or void), although it has the side-effect of writing
\p{
Hello, World!
}
\n to the standard output.
On the other hand,
\q{
"Hello, World!"
|evalsto "Hello, World!"
}
\n In either case, we are still at the listener. To exit,
type
\index{exit@\q{exit}}
\q{
(exit)
}
\n and this will land you back at the operating-system
command-line (which, as we’ve seen, is also a kind of
listener).
The listener is convenient for interactive testing of
programs and program fragments. However it is by no
means necessary. You may certainly stick to the
tradition of creating programs in their entirety in
files, and having Scheme execute them without any
explicit “listening”. In MzScheme, for instance, you
could say (at the operating-system prompt)
\p{
mzscheme -r hello.scm
}
\n and this will produce the greeting without making you
deal with
the listener. After the greeting, \p{mzscheme} will
return you to the
operating-system prompt. This is almost as if you said
\p{
echo Hello, World!
}
You could even make \p{hello.scm} seem like an
operating-system command (a shell script or a
batch file), but that will have to wait till
chapter~\ref{script}.
\endinput
One way to do this in the Unix version of
MzScheme is to place the following line at the head of
\p{hello.scm}:
\q{
":";exec mzscheme -r $0 $*
}
\n (This assumes your Unix command shell is either the
Bourne shell or \p{bash}.)
Make the file executable by doing \p{chmod +x
hello.scm}, and then simply type
\p{
hello.scm
}
\n at the Unix prompt.