When we write Scheme programs, we must follow a few carefully designed
rules, which are a compromise between a computer's capabilities and human
behavior.
Fortunately, forming Scheme definitions and
expressions is intuitive. Expressions are either ATOMIC,
that is,
numbers and variables; or they are COMPOUND
expressions, in which
case they start with ``('', followed by an operation, some more
expressions, and terminated by ``)''. Each expression in a compound
expression should be preceded by at least one space; line breaks are
permissible, and sometimes increase readability.
Definitions have the following schematic shape:
(define (f x ... y) an-expression)That is, a definition is a sequence of several words and expressions: ``('', the word ``define'', ``('', a non-empty sequence of names separated by spaces, ``)'', an expression, and a closing ``)''. The embedded sequence of names, f x ... y, introduces the name of the program and the names of its parameters.
Syntax Errors:
Not all parenthesized expressions are Scheme expressions. For
example, (10) is a parenthesized expression, but Scheme does not
accept it as a legal Scheme expression because numbers are not supposed to
be included in parentheses. Similarly, a sentence like (10 + 20)
is also ill formed; Scheme's rules demand that the operator is mentioned
first. Finally, the following two definitions are not well formed:
(define (P x) (+ (x) 10))The first one contains an extra pair of parentheses around the variable x, which is not a compound expression; the second contains two atomic expressions, x and 10, instead of one.(define (Q x) x 10)
When we click DrScheme's Execute button, the programming environment first determines whether the definitions are formed according to Scheme's rules. If some part of the program in the Definitions window is ill formed, DrScheme signals a SYNTAX ERROR with an appropriate error message and highlights the offending part. Otherwise it permits the user to evaluate expressions in the Interactions window.
Exercise 2.4.1
Evaluate the following sentences in DrScheme, one at a time:
(+ (10) 20) (10 + 20) (+ +)Read and understand the error messages. Solution
Exercise 2.4.2
Enter the following sentences, one by one, into DrScheme's Definitions window and click Execute:
(define (f 1) (+ x 10))Read the error messages, fix the offending definition in an appropriate manner, and repeat until all definitions are legal. Solution(define (g x) + x 10)
(define h(x) (+ x 10))
Run-time Errors: The evaluation of Scheme expressions
proceeds according to the intuitive laws of algebra and arithmetic.
When we
encounter new operations, we will extend these laws, first intuitively and
then, in section
, rigorously. For now, it is more important
to understand that not all legal Scheme expressions have a result. One
obvious example is (/ 1 0). Similarly, if we define
(define (f n) (+ (/ n 3) 2))we cannot ask DrScheme to evaluate (f 5 8).
When the evaluation of a legal Scheme expression demands a division by zero or similarly nonsensical arithmetic operations, or when a program is applied to the wrong number of inputs, DrScheme stops the evaluation and signals a RUN-TIME ERROR. Typically it prints an explanation in the Interactions window and highlights the faulty expression. The highlighted expression triggered the error signal.
Exercise 2.4.3
Evaluate the following grammatically legal Scheme expressions in DrScheme's Interactions window:
(+ 5 (/ 1 0))Read the error messages. Solution(sin 10 20)
(somef 10)
Exercise 2.4.4
Enter the following grammatically legal Scheme program into the Definitions window and click the Execute button:
(define (somef x) (sin x x))Then, in the Interactions window, evaluate the expressions:
(somef 10 20)and read the error messages. Also observe what DrScheme highlights. Solution(somef 10)
Logical Errors: A good programming environment assists the programmer in finding syntax and runtime errors. The exercises in this section illustrate how DrScheme catches syntax and run-time errors. A programmer, however, can also make LOGICAL ERRORS. A logical mistake does not trigger any error messages; instead, the program computes incorrect results. Consider the wage program from the preceding section. If the programmer had accidentally defined it as
(define (wage h) (+ 12 h))the program would still produce a number every time it is used. Indeed, if we evaluate (wage 12/11), we even get the correct result. A programmer can catch such mistakes only by designing programs carefully and systematically.