54
CHAPTER 4. SCHEME SERVLETS
4.7
The case expression
The echo servlet will behave poorly however if you don't give it any parameter.
It will usually just cause the browser to hang. To rectify this problem, we can
use the following nicer echo servlet:
; niceEcho.servlet
(servlet (a)
(case a
((#null){Please add "?a=Hi" to the URL and hit return})
(("Hi") {Good job. Now try "?a=Hello"})
(("Hello") {Great. Now try "?a=ANYTHING"})
(("ANYTHING") {OK, OK. Now try something more original})
(("original" "something") {Are you trying to be funny?})
(else {You just typed [a]})))
If one visits this page without an a parameter, then the value assigned to a
by the servlet will be the special value #null and it will return some instruc
tions about how it should be called. Otherwise, it will look at the value passed
in for a and if it matches one of the quoted expressions Hi , Hello etc. it
will return the corresponding expression. Note that original and something
both trigger the same response Are you trying to be funny? . If no match is
found, then the servlet just echoes back what the user entered.
The case expression in general has the form
(case a
((value1) result1)
((value2 value3) result2)
...
(else lastresult))
When the case expression is evaluated, the Scheme system tries to find the first
match of a with one of the values. When a match is found, the corresponding
result expression is evaluated. If no match is found then the else clause
applies and the lastresult expression is evaluated. The niceEcho.servlet
shows how to use the case to support an interaction between the user and the
servlet.
Exercise 7 What is the result of evaluating the following Scheme expressions:
(case "b"
(("a" "c") "hello")
(("b" "d") "goodbye")
(("a" "b") "wow")
(else "darn"))
(case (* 2 2)
((1 3 5 7 9) "odd digit")
Php Web Hosting Introduction to Interactive Web Design Php Hosting
|
|
|
|
TotalRoute.net Business web hosting division of Vision Web Hosting Inc. All rights reserved. |