The Grinnell Scheme Web:
Definitions

How does the programmer create new bindings?

This is what definitions are for. Recall that a Scheme program consists of definitions and commands. The definitions add variables to the top-level environment.

What does a definition look like?

A definition consists of a left parenthesis, the keyword define, a variable, an expression, and a right parenthesis. The value of the expression becomes the value of the variable.

Here's short Scheme program containing definitions:

> (define freezing 32)
> (define boiling 212)
> (- boiling freezing)
180
In this interaction, the programmer types in three lines. The first is a definition of the variable freezing, giving it the integer value 32. The second is a definition of the variable boiling, giving it the value 212. The third line is the command to subtract the first of these values from the second. In interactive mode, Scheme responds by printing out the difference.

Why didn't Scheme print out anything in response to either of the first two lines?

Definitions aren't commands. The Scheme standard doesn't say how Scheme is supposed to reply to a definition in interactive mode. Some implementations, such as Scheme 48, simply generate a new prompt, as shown above. Some, such as Elk, respond by echoing the variable that has been defined:

> (define freezing 32)
freezing
Still others print some fixed text acknowledging the input. SCM's fixed text is a reminder that the Scheme standard doesn't specify what should be printed:
> (define freezing 32)
#<unspecified>
> (define boiling 212)
#<unspecified>
What exactly happens when Scheme processes a top-level definition?

Scheme first computes the value of the expression, then checks the environment to see whether the variable has been defined previously. If so, it simply replaces the old value of the variable with the newly computed value of the expression -- in effect, it erases the old value from the index card for that variable and writes the new value in its place. If it doesn't find an index card for that variable -- that is, if the variable is unbound -- Scheme fills out a new card and adds it to the box, or in other words it adds a binding for that variable to the top-level environment.

Subsequently, when it's asked to evaluate an expression that contains the variable, Scheme consults the environment to determine the value. The most recent definition of the variable is the one that fixes its current value.

This account is slightly simplified, but it gives you a mental image that will work well enough for now and can be revised easily when we're ready to take on the additional complications.

Why do you need definitions? Couldn't you just use a numeral to express the value? In your example, there's no difference between (- boiling freezing) and (- 212 32), is there?

No, there's no difference. But definitions are a convenience to the programmer, for several reasons:

(1) The value of a variable can be the result of a long computation, which you wouldn't want to perform repeatedly. For example, suppose a programmer is working on a program that makes frequent use of the number of seconds in a week. You probably don't know how many seconds there are in a week -- I certainly don't -- but we can have Scheme compute the number and record it in the environment. Let's see: Seven days in a week, twenty-four hours in each day, sixty minutes in each hour, and sixty seconds in each minutes; the desired number of seconds is the product of those four numbers, so our definition looks like this:

(define seconds-in-week (* 7 24 60 60))
Now whenever we need this quantity, we can simply write in the variable seconds-in-week. (This is all one variable, by the way; it just happens to be one that is spelled with hyphens. The hyphens don't signify subtraction when they're used internally.)

(2) Often a variable is less confusing to a human reader of a program than the numeral of the same value would be. If the reader encounters the numeral 16777216 in a program, it may take him a long time to determine the significance of this ``magic number''; it is far kinder to call it by the name possible-pixel-colors instead.

(3) Using variables make it easier to maintain a program, and in particular to update it to reflect changes in the real world that it is supposed to model. For instance, if you've written a commercial program to do automatic typesetting with ten-point type, and the customer decides at the last minute that nine-point type would look better, you could be in trouble if you've used the numeral 10 for the type size throughout. You'll have to find every single occurrence of that numeral and change it to 9 -- except of course where it signifies the number of ligatures in the font or the width of a character or is used in the computation of page numbers. (Your customer will not be happy if all the page numbers are written in base 9.) If you've systematically used the variable type-size instead, you'll only have to change one line of your program -- the one that reads

(define type-size 10)
All the subsequent computations accommodate the change automatically.


Next topic
Previous topic
Table of contents


This document is available on the World Wide Web as

http://www.math.grin.edu/~stone/scheme-web/definitions.html


created May 23, 1995
last revised December 27, 1995

Copyright 1995 by John David Stone (stone@math.grin.edu)