Consider the following problem statement:
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, if the number is within the proper range.The italic words highlight the new part (compared to section
Conditions are nothing new. In mathematics we talk of true and false claims, which are conditions. For example, a number may be equal to, less than, or greater than some other number. If x and y are numbers, we state these three claims about x and y with
In addition to determining whether an atomic claim holds in a given situation, it is sometimes important to determine whether combinations of claims hold. Consider the three claims above, which we can combine in several ways:
Like mathematics, Scheme has ``words'' for expressing truth and falsity, for stating atomic claims, for combining claims into compound claims, and for expressing that a claim is true or false. The ``word'' for true is true and the ``word'' for false is false. If a claim concerns the relationship between two numbers, it can typically be expressed with a RELATIONAL OPERATION, for example, =, <, and >.
Translating the three mathematical claims from above follows our well-known pattern of writing a left parenthesis, followed by the operator, its arguments, and a right parenthesis:
A Scheme expression that compares numbers has a result just like any other Scheme expression. The result, however, is true or false, not a number. That is, when an atomic Scheme claim about two numbers is true, it evaluates to true. For example,
(< 4 5) = trueSimilarly, a false claim evaluates to false:
(= 4 5) = false
Expressing compound conditions in Scheme is equally natural. Suppose we want to combine (= x y) and (< y z) so that the compound claim holds if both conditions are true. In Scheme we would write
(and (= x y) (< y z))to express this relationship. Similarly, if we want to formulate a compound claim that is true if (at least) one of two claim holds, we write
(or (= x y) (< y z))Finally, when we write something such as
(not (= x y))we state that we wish the negation of a claim to be true.
Compound conditions, like atomic conditions, evaluate to true or false. Consider the following compound condition:
(and (= 5 5) (< 5 6))It consists of two atomic claims: (= 5 5) and (< 5 6). Both evaluate to true, and therefore the evaluation of the and-expression continues as follows:
... = (and true true) = trueThe last step follows because, if both parts of an and-expression are true, the entire expression evaluates to true. In contrast, if one of the two claims in an and-expression evaluates to false, the and-expression evaluates to false:
(and (= 5 5) (< 5 5)) = (and true false) = falseThe evaluation rules for or and not are similarly intuitive. The next few sections will explain why programming requires formulating conditions and reasoning about them.
Exercise 4.1.1
What are the results of the following Scheme conditions?
Exercise 4.1.2