These days computers mostly process symbolic information such as names, words, directions, or images. All modern programming languages support at least one way of representing symbolic information. Scheme supports several ways to express symbolic information: symbols, strings, (keyboard) characters, and images. A symbol is a sequence of keyboard characters16 preceded by a single forward quotation mark:
'eastwill usually refer to the direction where the sun rises,
'professorwill be the title of a person teaching and researching at a university.
Like numbers, symbols are atomic pieces of data. Their purpose is to
represent things such as family and first names, job titles, commands,
announcements, and so on. Scheme provides only one basic operation on
symbol=?, a comparison operation. It consumes two
symbols and produces
true if and only if the two symbols are
(symbol=? 'Hello 'Hello) = true
(symbol=? 'Hello 'Howdy) = false
(symbol=? 'Hello x) = true if
x stands for
(symbol=? 'Hello x) = false if
x stands for
Symbols were first introduced to computing by researchers in artificial
intelligence who wanted to design functions that could have conversations
with people. Consider the function
reply, which replies with some
remark to the following greetings: ``good morning,'' ``how are you,''
``good afternoon,'' and ``good evening.'' Each of those short sentences
can be represented as a symbol:
reply consumes a symbol and replies with a symbol:
reply : symbol -> symbol;; to determine a reply for the greeting
s(define (reply s) ...)
Furthermore, the function must distinguish among four situations, implying, according to our design recipe from section 4.4, a four-clause cond-expression:
(define (reply s) (cond [(symbol=? s 'GoodMorning) ...] [(symbol=? s 'HowAreYou?) ...] [(symbol=? s 'GoodAfternoon) ...] [(symbol=? s 'GoodEvening) ...]))
cond-clauses match the four symbols, which is naturally much
easier than matching four intervals.
From this function template it is a short step to the final
function. Here is one version of
(define (reply s) (cond [(symbol=? s 'GoodMorning) 'Hi] [(symbol=? s 'HowAreYou?) 'Fine] [(symbol=? s 'GoodAfternoon) 'INeedANap] [(symbol=? s 'GoodEvening) 'BoyAmITired]))
We can think of many different ways of how to replace the ``...'' in the template with replies. But no matter what we replace them with, the basic template could be defined without concern for the output of the function. We will see in subsequent sections that this focus on the input data is actually the norm and that concern for the output data can be postponed.
A Note on Strings: A string is a second form of symbolic data. Like a symbol, a string consists of a sequence of keyboard characters, but they are enclosed in string quotes:
string=?, which compares two strings the way
symbol=?compares two symbols. Otherwise we ignore strings, and when we use them, we act as if they were symbols.
A Note on Images: An image is a third form of symbolic data, and it is fun to develop functions that process images. Like symbols, images don't have any a priori meaning, but we tend to connect them easily with the intended information.
DrScheme supports images: see figure 7, which shows the beginning of a function that manipulates planet pictures. Images are values like numbers and booleans. They can therefore be used inside of expressions. Most often though, we give images names because they are typically used by several functions. If we don't like the picture, it is then easily replaced with a different one (see section 3.2).
(reply 'HowAreYou?) by hand and with DrScheme's
stepper. Formulate a complete set of examples for
reply as boolean
Develop the function
check-guess. It consumes two numbers,
target. Depending on how
guess relates to
the function produces one of the following three answers:
The function implements one part of a two-player number guessing game. One player
picks a random number between 0 and 99999. The other player's goal is to
determine this number, called
target, with the least number of
guesses. To each guess, the first player responds with one of the three responses
check-guess and the teachpack guess.ss implement the
first player. The teachpack picks the random number, pops up a window in which
the second player can choose digits, and hands over the
guess and the
check-guess. To play the game, set the
teachpack to guess.ss using the
Language|Set teachpack option.
Then evaluate the expression
check-guess has been thoroughly tested.
Develop the function
check-guess3. It implements a larger portion of
the number guessing game of exercise 5.1.2 than the function
check-guess. Now the teachpack hands over the
the user guesses, not the number that they form.
To simplify the problem a little bit, the game works with only three
check-guess3 consumes three digits and a number. The
first digit is the least significant, the third one is the most significant. The
number is called
target and represents the randomly chosen number.
Depending on how
guess, the number determined by the three digits,
check-guess3 produces one of the following
The rest of the game is still implemented by guess.ss. To play
the game with
after the function has been thoroughly tested.
Hint: Remember to develop an auxiliary function per concept. Solution
what-kind. The function consumes the coefficients
c of a quadratic equation. It then determines whether
the equation is degenerate and, if not, how many solutions the equation has. The
function produces one of four symbols:
Hint: Compare with exercise 4.4.4. Solution
Develop the function
check-color. It implements a key portion of a
color guessing game. One player picks two colors for two squares; we call
The other one tries to guess which color is
assigned to which square; they are guesses. The first player's response
to a guess is to check the colors and to produce one of the following
'Perfect, if the first target is equal to the first
guess and the second target is equal to the second guess;
'OneColorAtCorrectPosition, if the first guess is equal to
the first target or the second guess is equal to the second target;
'OneColorOccurs, if either guess is one of the two targets;
These four answers are the only answers that the first player gives. The second player is to guess the two chosen target colors with as few guesses as possible.
check-color simulates the first player's checking action. It
consumes four colors; for simplicity, we assume that a color is a symbol, say,
'red. The first two arguments to
check-color are ``targets,''
the latter two are ``guesses.'' The function produces one of the four answers.
When the function is tested, use the teachpack to master.ss to play
the color-guessing game.17 The teachpack
provides the function
and choose colors with the mouse.