A critical aspect of a programming language is the means it provides
for using
names to refer to computational
objects.
objects, and our first such means are constants.
We say that the
name identifies a
variable
constant
whose
value is the object.
In the Scheme dialect of Lisp, we name things with
define.
In JavaScript, we name constants with
constant declarations.
Original |
JavaScript |
(define size 2)
|
const size = 2;
|
causes the interpreter to associate the value 2 with the
name
size.
Once the name
size
has been associated with the number 2, we can
refer to the value 2 by name:
Original |
JavaScript |
size
2
|
size;
2
|
Original |
JavaScript |
(* 5 size)
10
|
5 * size;
10
|
Original |
|
JavaScript |
|
|
The JavaScript interpreter needs to execute the constant
declaration for size
before the name size can be used
in an expression. In this online book, the statements that need to be
evaluated before a new statement are omitted for brevity. However,
to see and play with the program, you can click on it. The
program then appears in a new
browser tab, with the option Show Dependencies .
Thus, as a result of clicking on
5 * size;
a new tab appears that contains the program, and after clicking
Show Dependencies , you see:
const size = 2;
5 * size;
|
Here are further examples of the use of
define:
const:
Original |
JavaScript |
(define pi 3.14159)
|
const pi = 3.14159;
|
Original |
JavaScript |
(define radius 10)
|
const radius = 10;
|
Original |
JavaScript |
(* pi (* radius radius))
314.159
|
pi * radius * radius;
314.159
|
Original |
JavaScript |
(define circumference (* 2 pi radius))
|
const circumference = 2 * pi * radius;
|
Original |
JavaScript |
circumference
62.8318
|
circumference;
62.8318
|
Define
Constant
declaration
is our language's
simplest means of abstraction, for it allows us to use simple names to
refer to the results of compound operations, such as the
circumference computed above.
In general, computational objects may have very complex
structures, and it would be extremely inconvenient to have to remember
and repeat their details each time we want to use them. Indeed,
complex programs are constructed by building, step by step,
computational objects of increasing complexity. The
interpreter makes this step-by-step program construction particularly
convenient because name-object associations can be created
incrementally in successive interactions. This feature encourages the
incremental development and testing of programs and is largely
responsible for the fact that a
Lisp
JavaScript
program usually consists of a large
number of relatively simple
procedures.
functions.
It should be clear that the possibility of associating values with
names and later retrieving them means that the interpreter must
maintain some sort of memory that keeps track of the name-object
pairs. This memory is called the
environment
(more precisely the
global environment,
program environment,
since we will see later that a
computation may involve a number of different
environments).
1.1.2 Naming and the Environment