Section 5

Symbolic Information


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:

'the      'dog      'ate     'a     'chocolate     'cat!     'two^3     'and%so%on?     
Like a number, a symbol has no inherent meaning. It is up to the function's user to relate symbolic data and real-world information, though the connection is typically obvious in a specific context. For example, 'east will usually refer to the direction where the sun rises, 'professor will be the title of a person teaching and researching at a university.


[planets in DrScheme]

Figure 7:  The planets as images in DrScheme

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 symbols: symbol=?, a comparison operation. It consumes two symbols and produces true if and only if the two symbols are identical:

  1.    (symbol=? 'Hello 'Hello) = true

  2.    (symbol=? 'Hello 'Howdy) = false

  3.    (symbol=? 'Hello x) = true if x stands for 'Hello

  4.    (symbol=? 'Hello x) = false if x stands for 'Howdy

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: 'GoodMorning, 'HowAreYou, 'GoodAfternoon, and 'GoodEvening. Thus, 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)
    [(symbol=? s 'GoodMorning) ...]
    [(symbol=? s 'HowAreYou?) ...]
    [(symbol=? s 'GoodAfternoon) ...]
    [(symbol=? s 'GoodEvening) ...]))

The 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 reply:

(define (reply s)
    [(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:

"the dog"      "isn't"     "made of"     "chocolate"     "two^3"     "and so on?"     
In contrast to symbols, strings are not atomic. They are compound data, which we discuss later in the book. For now, we use strings as if they were fancy symbols; the only operation needed is 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). 

5.1  Finger Exercises with Symbols


Exercise 5.1.1.   Evaluate (reply 'HowAreYou?) by hand and with DrScheme's stepper. Formulate a complete set of examples for reply as boolean expressions (using symbol=?).    Solution

Exercise 5.1.2.   Develop the function check-guess. It consumes two numbers, guess and target. Depending on how guess relates to target, the function produces one of the following three answers: 'TooSmall, 'Perfect, or 'TooLarge.

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 that check-guess implements.

The function check-guess and the teachpack 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 target to check-guess. To play the game, set the teachpack to using the Language|Set teachpack option. Then evaluate the expression

(guess-with-gui check-guess)

after check-guess has been thoroughly tested.    Solution

Exercise 5.1.3.   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 digits that the user guesses, not the number that they form.

To simplify the problem a little bit, the game works with only three numbers. Thus, 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, relates to target, check-guess3 produces one of the following three answers: 'TooSmall, 'Perfect, or 'TooLarge.

The rest of the game is still implemented by To play the game with check-guess3, evaluate

(guess-with-gui-3 check-guess3)

after the function has been thoroughly tested.

Hint: Remember to develop an auxiliary function per concept.    Solution

Exercise 5.1.4.   Develop what-kind. The function consumes the coefficients a, b, and 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: 'degenerate, 'two, 'one, or 'none.

Hint: Compare with exercise 4.4.4.    Solution

Exercise 5.1.5.   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 those targets. 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 answers:

  1. 'Perfect, if the first target is equal to the first guess and the second target is equal to the second guess;

  2. 'OneColorAtCorrectPosition, if the first guess is equal to the first target or the second guess is equal to the second target;

  3. 'OneColorOccurs, if either guess is one of the two targets; and

  4. 'NothingCorrect, otherwise.

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.

The function 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 to play the color-guessing game.17 The teachpack provides the function master. Evaluate (master check-color) and choose colors with the mouse.    Solution

16 Not all keyboard characters are legal in symbols. For example, a blank space or a comma are illegal.

17 MasterMind, the commercial version of this game, is played in a different manner.