Programmers are rarely handed mathematical expressions to turn into programs. Instead they typically receive informal problem descriptions that often contain irrelevant and sometimes ambiguous information. The programmers' first task is to extract the relevant information and then to formulate appropriate expressions.
Here is a typical example:
Company XYZ & Co. pays all its employees $12 per hour. A typical employee works between 20 and 65 hours per week. Develop a program that determines the wage of an employee from the number of hours of work.
The last sentence is the first to mention the actual task: to write a program that
determines one quantity based on some other quantity. More specifically, the program consumes one quantity, the number of hours of work, and produces another one, the wage in dollars. The first sentence implies how to compute the result, but doesn't state it explicitly. In this particular example, though, this poses no problem. If an employee works h hours, the wage is
Now that we have a rule, we can formulate a Scheme program:
(define (wage h) (* 12 h))
The program is called wage; its parameter h stands for the hours an employee works;
and its result is (* 12 h), the corresponding wage.
Exercise 2.3.1. Utopia's tax accountants always use programs that compute income taxes even though the tax rate is a solid, never-changing 15%. Define the program tax, which determines the tax on the gross pay.
Also define netpay. The program determines the net pay of an employee from the number of hours worked. Assume an hourly rate of $12. Solution
Exercise 2.3.2. The local supermarket needs a program that can compute the value of a bag of coins. Define the program sum-coins. It consumes four numbers: the number of pennies, nickels, dimes, and quarters in the bag; it produces the amount of money in the bag. Solution
Exercise 2.3.3. An old-style movie theater has a simple profit function. Each customer pays $5 per ticket. Every performance costs the theater $20, plus $.50 per attendee.
Develop the function total-profit. It consumes the number of attendees (of a show) and produces how much income the attendees produce. Solution
2.4
Errors
Errors
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.7 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:8 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)) (define (Q 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.
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.
Errors
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))
(define (g x) + x 10) (define h(x) (+ x 10))
Read the error messages, fix the offending definition in an appropriate manner, and repeat until all definitions are legal. Solution
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 8, 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-TIMEERROR. 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)) (sin 10 20) (somef 10)
Read the error messages. Solution
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) (somef 10)
and read the error messages. Also observe what DrScheme highlights. Solution
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 LOGICALERRORS. 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.