[previous] [up] [next]     [index]
Next: Finger Exercises with Symbols Up: Processing Simple Forms of Previous: Designing Conditional Functions

Symbolic Information

external

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 characters[footnote] 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: 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:

(symbol=? 'Hello 'Hello) = true
(symbol=? 'Hello 'Howdy) = false
(symbol=? 'Hello x) = true if x stands for 'Hello
(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 [cross-reference], a four-clause cond-expression:
(define (reply s)
  (cond 
    [(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)
  (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: ``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 [cross-reference], 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 [cross-reference]). 





[previous] [up] [next]     [index]
Next: Finger Exercises with Symbols Up: Processing Simple Forms of Previous: Designing Conditional Functions

PLT