3.5 Definitions

Scheme provides a simple, yet powerful, mechanism for abstraction. A definition introduces a new name and gives it a value:

Definition ::$\Rightarrow$  (define Name Expression)

After a definition, the Name in the definition is now associated with the value of the expression in the definition. A definition is not an expression since it does not evaluate to a value.

A name can be any sequence of letters, digits, and special characters (such as -, >, ?, and !) that starts with a letter or special character. Examples of valid names include a, Ada, Augusta-Ada, gold49, !yuck, and yikes!%@\#. We don’t recommend using some of these names in your programs, however! A good programmer will pick names that are easy to read, pronounce, and remember, and that are not easily confused with other names.

After a name has been bound to a value by a definition, that name may be used in an expression:

Expression :: $\Rightarrow$ NameExpression
NameExpression :: $\Rightarrow$ Name

The value of a NameExpression is the value associated with the Name. (Alert readers should be worried that we need a more precise definition of the meaning of definitions to know what it means for a value to be associated with a name. This informal notion will serve us well for now, but we will need a more precise explanation of the meaning of a definition in Chapter 9.)

Below we define speed-of-light to be the speed of light in meters per second, define seconds-per-hour to be the number of seconds in an hour, and use them to calculate the speed of light in kilometers per hour:

Scheme output
> (define speed-of-light 299792458)
> speed-of-light
299792458
> (define seconds-per-hour (* 60 60))
> (/ (* speed-of-light seconds-per-hour) 1000)
1079252848 4/5