4.10. THE IF FORM AND CONDITIONAL EXECUTION
57
where the Exprs are evaluated and stored in the corresponding variables var1,....
These variables are then used in the final Expr to compute the value returned
by the let* expression. The key points to note here are that each variable can
be used to define the values of the following variables, but none of the variables
has a value outside of the let*. Note: there is also a let expression which is
almost exactly the same, except that the defined variables cannot depend on
each other.
Exercise 10 Write a servlet that uses a let* expression to compute the area
of a triangle whose sides are a, b, and c using the following formula:
A =
(s - a)(s - b)(s - c)s
where s = (a + b + c)/2 is the semiperimeter. You should try this with a = 3,
b = 4, and c = 5. These are the sides of a 3 4 5 right triangle and so the areas
should be half of base times width, which is 0.5   3   4 = 6.
4.10
The if form and conditional execution
As a final example of these simple servlets, we now consider a liquor test servlet
which determines whether you can buy liquor in Massachusetts.
; liquorTest.servlet
(case (age)
((#null) {Enter your age by appending ?age=19 to the URL})
(else
(if (< age 21)
{Sorry, you have to wait [(  21 age)] years before
you can buy alcohol here.}
{You've been able to buy liquor here for [(  age 21)]
years})))
This servlet uses the (if TEST THEN ELSE form to test whether the buyer's age
is 21 or higher. This form first evaluates the TEST. If the result is false, it then
evaluates the ELSE code, otherwise it evaluates the THEN code. This example
also introduces the comparison operator <. Scheme has a rich set of operators
for creating tests. The numeric comparison operators you can use are
(<
a b)
   true if a is less than b
(<= a b)
   true if a is less than or equal to b
(>
a b)
   true if a is greater than b
(>= a b)
   true if a is greater than or equal to b
(=
a b)
   true if a is equal to b
(!= a b)
   true if a is not equal to b
In addition, the arguments a and b can be arithmetic expressions (like (/ age
10)).
You can also combine tests using the and, or, and not operators, e.g. to test
if someone is  college age  you could use the following expression:




Php Web Hosting Introduction to Interactive Web Design Php Hosting




 
TotalRoute.net Business web hosting division of Vision Web Hosting Inc. All rights reserved.