56
CHAPTER 4. SCHEME SERVLETS
Exercise 8 Write a servlet sumtoN.servlet which finds the sum of the num
bers from 1 to N using the formula
1 + 2 + 3 + . . . + N = N (N + 1)/2
and use it to find the sum from 1 to 100 (You should get 5050).
Exercise 9 Write a servlet tip.servlet which calculates the tip required for a
meal assuming that you tip at the 15% rate.
4.9
Giving names to values using let*
When processing complex formulas it is often helpful to break the formula into
pieces and to give names to each individual piece. For example, the body mass
index provides a rough measure of whether your are overweight. The formula
consists of dividing your weight in kilograms by the square of your height in
meters. The following servlet computes your BMI by first converting your weight
in pounds to your weight in kilograms, and your height in inches to your height
in meters, then it applies the formula:
; BMI.servlet
(servlet (w h)
(case w
((#null) {Enter data using ?w=170&h=68})
(else
(let* (
(k
(/ w 2.2))
; weight in kilograms
(m (/ h 39.36))
; height in meters
(bmi (/ k (* m m)))
)
{If your weight is [w] pounds
and your height is [h] inches,
Then your BMI is [bmi].
Note: a BMI over 30 is considered
medically dangerous}))))
The let* expression above provides temporary names k, m, and bmi for the
computed values. The general form of a let* statement is
(let* (
(var1 Expr1)
(var2 Expr2)
...
(varn Exprn)
)
Expr)
Php Web Hosting Introduction to Interactive Web Design Php Hosting
|
|
|
|
TotalRoute.net Business web hosting division of Vision Web Hosting Inc. All rights reserved. |