58
CHAPTER 4. SCHEME SERVLETS
(and (<= age 22) (>= age 16))
To test whether someone is not college age, you could use either
(not (and (<= age 22) (>= age 16)))
or
(or (> age 22) (< age 16))
which are equivalent for any numeric value of age.
Exercise 11 What is the result of evaluating the following Scheme expressions:
(if (< 5 (* 2 4))
(* 23 20)
( 18 5))
(if (> (+ 23 9) (* 6 5))
{Cool! [(+ 23 9)] rules!}
{Hmmm, [(* 6 5)] is biggest})
Exercise 12 Write a servlet that determines whether someone is a senior cit
izen by testing if their age is at least 60.
4.11
The cond form and multiple tests
Sometimes one wants to combine several tests and do something different for
each of the possible test results. For example, one might want to classify some
one as a child, teenager, adult, or senior citizen based on their age. The simplest
way to do this is using the cond form, as in the following servlet
; ageClassifier.servlet
(servlet (age)
(cond
((< age 13) "child")
((< age 20) "teenager")
((< age 60) "adult")
(else "senior citizen")))
The cond expression consists of several clauses each of which begins with a
test. If the test is true, then the rest of the clause is evaluated. Otherwise,
testing continues with the next clause. The very last clause should always be
an else clause which applies to all remaining cases. The tests can be simple
comparisons as shown above, or can be very complex expressions.
Exercise 13 Write a servlet which computes the tip given the cost of the meal
and a number between 0.0 and 10.0 representing the quality of service. You
should use a cond to determine whether to leave no tip, a 5%, 10%, 15%, or
20% tip based on the service number.