Now that you're familiar with typing in erroneous expressions, let's get back to trying legal ones.
If you've exited your Scheme system, fire it up again.
Type in the addition expression
(+ 2 3), and hit
(From now on, I'll skip saying "and hit
<return>." I'll also stop
showing the prompt Scheme gives you after printing the result of
Scheme>(+ 2 3) 5
Again, Scheme evaluated the expression, printed the result, which was
(a pointer to)
5, and gave you a prompt to type in something else.
Notice that it didn't save the value anywhere. It just printed out the
The value we gave to
myvar earlier is still there, though. We can
ask Scheme what it is, just by typing in a variable reference expression,
i.e., just the variable name.
Scheme has kept track of the storage named
myvar, and it evaluates
myvar by looking up the value. Then it prints out
that result, and gives you another prompt, as it always does.
To change the value stored in the binding of
myvar, and look at the
new value, just type in a
set! expression and then the name of the
variable, like this:
Scheme>(set! myvar 32) #void Scheme>myvar 32
You may see a different result for the
set! expression. Standard
Scheme doesn't specify the return value of
set!, because you
generally use it for its side-effect, not its result. As with
define, your system may return something different. It may
also suppress the printing of this useless value, so you may not see
anything at all.
In some Scheme systems, the value of a
set! expression is the
name of the variable being set, so you may see somthing like this:
Scheme>(set! myvar 32) myvar Scheme>myvar 32
(In other systems, it's something else,
like the old value of the variable you're clobbering.) You should not
depend on the value returned by the
set! if you want your program
to be portable. In the example above, it doesn't really matter what
set! returns, except that that's what gets printed
out before you get a new prompt. What matters about
its effect, which is to update the value of the variable binding.
As we can see, it had its effect--when we evaluate the expression
myvar, it returns the new value, which is printed out:
We can also use more complicated expressions--just about anything. Now we'll increment the variable by five, and again ask Scheme the value of the variable.
Scheme>(set! myvar (+ myvar 5)) #void Scheme>myvar 37
Now let's define a procedure that expects a number as its argument, and
returns a number that's twice as big. Then we'll call it with the argument
Scheme>(define (double x) (+ x x)) #void Scheme>(double 2) 4
After evaluating the first expression, Scheme keeps track of the
double. When we type in the second expression,
Scheme calls that procedure, which returns a result, which Scheme prints
Since Scheme keeps track of the variables and values we typed in earlier,
we can call
double to double the value of myvar:
Scheme>(double myvar) 74
We can define new procedures in terms of old ones. (Actually, we did
this when we defined
double---it's defined in terms of
which is predefined, i.e., Scheme knows that definition when it starts
Scheme>(define (quadruple x) (double (double x))) #void Scheme>(quadruple 15) 60
Now try using the predefined Scheme procedure
Scheme>(display "Hello, world!") Hello, world! #void
display had the side-effect of printing
to the screen, and returned the value
void#, which was printed.
What you see on the screen may vary in a couple of ways, neither of
which is worrisome. Your system may have printed the return value
on the same line as the (side-effect) output of
a linebreak. Since the main use of
display is for its effect,
its return value is undefined, so you may see something other than
#void, or nothing at all. You might see this:
Scheme>(display "Hello, world!") Hello, world! "Hello, world"
If you do, it means that in your system
display returns the object
you asked it to display. Then Scheme prints out that return value, with
double quotes to tell you it's a string object. This shouldn't be too
surprising--remember that Scheme prints out the return values of expressions
after evaluating them.
displaying a number:
Scheme>(display 322) 322 #void