Symbolic Information

#drnsecsym#3346> 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 <#60844#><#3347#>SYMBOL<#3347#><#60844#> is a sequence of keyboard characters preceded by a single forward quotation mark: <#60845#><#3350#>'<#3350#><#3351#>the<#3351#><#60845#> <#60846#><#3352#>'<#3352#><#3353#>dog<#3353#><#60846#> <#60847#><#3354#>'<#3354#><#3355#>ate<#3355#><#60847#> <#60848#><#3356#>'<#3356#><#3357#>a<#3357#><#60848#> <#60849#><#3358#>'<#3358#><#3359#>chocolate<#3359#><#60849#> <#60850#><#3360#>'<#3360#><#3361#>cat!<#3361#><#60850#> <#60851#><#3362#>'<#3362#><#3363#>two^<#3363#><#3364#>3<#3364#><#60851#> <#60852#><#3365#>'<#3365#><#3366#>and%<#3366#><#3367#>so%<#3367#><#3368#>on?<#3368#><#60852#> 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, <#60853#><#3370#>'<#3370#><#3371#>east<#3371#><#60853#> will usually refer to the direction where the sun rises, <#60854#><#3372#>'<#3372#><#3373#>professor<#3373#><#60854#> will be the title of a person teaching and researching at a university.
~


rawhtml12

<#3374#>Figure: The planets as images in DrScheme<#3374#>


Like numbers, symbols are atomic pieces of data. Their purpose is to represent things such as family and first names, job titles, commands, and announcements, and so on. Scheme provides only one basic operation on symbols: <#60855#><#3376#>symbol=?<#3376#><#60855#>, a comparison operation. It consumes two symbols and produces <#60856#><#3377#>true<#3377#><#60856#> if and only if the two symbols are identical:
<#60857#><#3379#>(symbol=?<#3379#>\ <#3380#>'<#3380#><#3381#>Hello<#3381#>\ <#3382#>'<#3382#><#3383#>Hello)<#3383#>\ <#3384#>=<#3384#>\ <#3385#>true<#3385#><#60857#>
<#60858#><#3386#>(symbol=?<#3386#>\ <#3387#>'<#3387#><#3388#>Hello<#3388#>\ <#3389#>'<#3389#><#3390#>Howdy)<#3390#>\ <#3391#>=<#3391#>\ <#3392#>false<#3392#><#60858#>
<#60859#><#3393#>(symbol=?<#3393#>\ <#3394#>'<#3394#><#3395#>Hello<#3395#>\ <#3396#>x)<#3396#>\ <#3397#>=<#3397#>\ <#3398#>true<#3398#><#60859#> if <#60860#><#3399#>x<#3399#><#60860#> stands for <#60861#><#3400#>'<#3400#><#3401#>Hello<#3401#><#60861#>
<#60862#><#3402#>(symbol=?<#3402#>\ <#3403#>'<#3403#><#3404#>Hello<#3404#>\ <#3405#>x)<#3405#>\ <#3406#>=<#3406#>\ <#3407#>false<#3407#><#60862#> if <#60863#><#3408#>x<#3408#><#60863#> stands for <#60864#><#3409#>'<#3409#><#3410#>Howdy<#3410#><#60864#>
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 <#60865#><#3412#>reply<#3412#><#60865#>, 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: <#60866#><#3413#>'<#3413#><#3414#>GoodMorning<#3414#><#60866#>, <#60867#><#3415#>'<#3415#><#3416#>HowAreYou<#3416#><#60867#>, <#60868#><#3417#>'<#3417#><#3418#>GoodAfternoon<#3418#><#60868#>, and <#60869#><#3419#>'<#3419#><#3420#>GoodEvening<#3420#><#60869#>. Thus, <#60870#><#3421#>reply<#3421#><#60870#> consumes a symbol and replies with a symbol:
<#70729#>;; <#60871#><#3426#>reply<#3426#> <#3427#>:<#3427#> <#3428#>symbol<#3428#> <#3429#><#3429#><#3430#>-;SPMgt;<#3430#><#3431#><#3431#> <#3432#>symbol<#3432#><#60871#><#70729#>
<#70730#>;; to determine a reply for the greeting <#60872#><#3433#>s<#3433#><#60872#><#70730#> 
<#3434#>(define<#3434#> <#3435#>(reply<#3435#> <#3436#>s)<#3436#> <#3437#>...)<#3437#> 
Furthermore, the function must distinguish among four different situations. This means our design recipe from section~#secdesign2#3441> applies. According to this design recipe, we need a four-clause <#60873#><#3442#>cond<#3442#>-expression<#60873#>:
<#3447#>(d<#3447#><#3448#>efine<#3448#> <#3449#>(reply<#3449#> <#3450#>s)<#3450#>
  <#3451#>(c<#3451#><#3452#>ond<#3452#> 
    <#3453#>[<#3453#><#3454#>(symbol=?<#3454#> <#3455#>s<#3455#> <#3456#>'<#3456#><#3457#>GoodMorning)<#3457#> <#3458#>...]<#3458#> 
    <#3459#>[<#3459#><#3460#>(symbol=?<#3460#> <#3461#>s<#3461#> <#3462#>'<#3462#><#3463#>HowAreYou?<#3463#><#3464#>)<#3464#> <#3465#>...]<#3465#> 
    <#3466#>[<#3466#><#3467#>(symbol=?<#3467#> <#3468#>s<#3468#> <#3469#>'<#3469#><#3470#>GoodAfternoon)<#3470#> <#3471#>...]<#3471#> 
    <#3472#>[<#3472#><#3473#>(symbol=?<#3473#> <#3474#>s<#3474#> <#3475#>'<#3475#><#3476#>GoodEvening)<#3476#> <#3477#>...]<#3477#><#3478#>))<#3478#> 
The <#60874#><#3482#>cond<#3482#><#60874#>-clauses match the four symbols, which is naturally much easier than matching four intervals. From this function <#60875#><#3483#>TEMPLATE<#3483#><#60875#> it is a short step to the final function. Here is one version of <#60876#><#3484#>reply<#3484#><#60876#>:
<#3489#>(d<#3489#><#3490#>efine<#3490#> <#3491#>(reply<#3491#> <#3492#>s)<#3492#>
  <#3493#>(c<#3493#><#3494#>ond<#3494#> 
    <#3495#>[<#3495#><#3496#>(symbol=?<#3496#> <#3497#>s<#3497#> <#3498#>'<#3498#><#3499#>GoodMorning)<#3499#> <#3500#>'<#3500#><#3501#>Hi]<#3501#> 
    <#3502#>[<#3502#><#3503#>(symbol=?<#3503#> <#3504#>s<#3504#> <#3505#>'<#3505#><#3506#>HowAreYou?<#3506#><#3507#>)<#3507#> <#3508#>'<#3508#><#3509#>Fine]<#3509#> 
    <#3510#>[<#3510#><#3511#>(symbol=?<#3511#> <#3512#>s<#3512#> <#3513#>'<#3513#><#3514#>GoodAfternoon)<#3514#> <#3515#>'<#3515#><#3516#>INeedANap]<#3516#> 
    <#3517#>[<#3517#><#3518#>(symbol=?<#3518#> <#3519#>s<#3519#> <#3520#>'<#3520#><#3521#>GoodEvening)<#3521#> <#3522#>'<#3522#><#3523#>BoyAmITired]<#3523#><#3524#>))<#3524#> 
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. <#3528#>A Note on Strings<#3528#>: A <#60877#><#3529#>STRING<#3529#><#60877#> 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: <#60878#><#3531#>``the<#3531#>\ <#3532#>dog''<#3532#><#60878#> <#60879#><#3533#>``isn'<#3533#><#3534#>t''<#3534#><#60879#><#60880#><#3535#>``made<#3535#>\ <#3536#>of''<#3536#><#60880#> <#60881#><#3537#>``chocolate''<#3537#><#60881#><#60882#><#3538#>``two^<#3538#><#3539#>3''<#3539#><#60882#><#60883#><#3540#>``and<#3540#>\ <#3541#>so<#3541#>\ <#3542#>on?<#3542#><#3543#>''<#3543#><#60883#> 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 <#60884#><#3545#>string=?<#3545#><#60884#>, which compares two strings the way <#60885#><#3546#>symbol=?<#3546#><#60885#> compares two symbols. Otherwise we ignore strings, and when we use them, we act as if they were symbols.~<#60886#><#60886#> <#3549#>A Note on Images<#3549#>: An <#60887#><#3550#>IMAGE<#3550#><#60887#> is a third form of symbolic data, and it is fun to develop functions that process images. Like symbols, images don't have any <#3551#>a priori<#3551#> meaning, but we tend to connect them easily with the intended information. DrScheme supports images: see figure~#figplanetspics#3552>, 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~#secusevardefs#3553>).~<#60888#><#60888#>