The Meaning of Scheme

#drnsecsynsemsem#7619> A legal DrScheme program consists of two items: a sequence of function definitions (in the <#7620#>Definitions<#7620#> window) and a sequence of interactions (in the <#7621#>Interactions<#7621#> window). Each interaction is a demand for the evaluation of one Scheme expression, which typically refers to the functions defined in the upper part of DrScheme. When DrScheme evaluates an expression, it uses nothing but the laws of arithmetic and algebra to convert an expression into a value. In ordinary mathematics courses, values are just numbers. We also include symbols, booleans, and indeed all constants:

#tabular7623#

The collection of values is thus just a subset of the collection of expressions. Now that we have defined the set of values, it is easy to introduce and to explain the evaluation rules. The rules come in two categories: those that appeal to arithmetic knowledge and those that rely on a small amount of algebra. First, we need an infinite number of rules like those of arithmetic to evaluate applications of primitives:

<#7633#>(+<#7633#> <#7634#>1<#7634#> <#7635#>1)<#7635#> <#7636#>=<#7636#> <#7637#>2<#7637#>
<#7638#>(-<#7638#> <#7639#>2<#7639#> <#7640#>1)<#7640#> <#7641#>=<#7641#> <#7642#>1<#7642#> 
But Scheme ``arithmetic'' is more general than just number crunching. It also includes rules for dealing with Boolean values, symbols, and lists like these:
<#7650#>(not<#7650#> <#7651#>true<#7651#><#7652#>)<#7652#> <#7653#>=<#7653#> <#7654#>false<#7654#>
<#7655#>(symbol=?<#7655#> <#7656#>'<#7656#><#7657#>a<#7657#> <#7658#>'<#7658#><#7659#>b)<#7659#> <#7660#>=<#7660#> <#7661#>false<#7661#> 
<#7662#>(symbol=?<#7662#> <#7663#>'<#7663#><#7664#>a<#7664#> <#7665#>'<#7665#><#7666#>a)<#7666#> <#7667#>=<#7667#> <#7668#>true<#7668#> 
Second, we need one rule from algebra to understand how the application of a user-defined function advances computation. Suppose the <#7672#>Definitions<#7672#> window contains the definition
<#7677#>(d<#7677#><#7678#>efine<#7678#> <#7679#>(f<#7679#> <#7680#>x-1<#7680#> <#7681#>...<#7681#> <#7682#>x-n)<#7682#> 
  <#7683#>exp)<#7683#> 
and <#61788#><#7687#>f,<#7687#>\ <#7688#>x-1,<#7688#>\ <#7689#>...,<#7689#>\ <#7690#>x-n<#7690#><#61788#> are variables and <#61789#><#7691#>exp<#7691#><#61789#> is some (legal) expression. Then an application of a function is governed by the law:

#tabular7693#

where <#61794#><#7710#>v-1<#7710#>\ <#7711#>...<#7711#>\ <#7712#>v-n<#7712#><#61794#> is a sequence of values that is as long as <#61795#><#7713#>x-1<#7713#>\ <#7714#>...<#7714#>\ <#7715#>x-n<#7715#><#61795#>. This rule is as general as possible, so it is best to look at a concrete example. Say the definition is

<#7720#>(d<#7720#><#7721#>efine<#7721#> <#7722#>(poly<#7722#> <#7723#>x<#7723#> <#7724#>y)<#7724#> 
  <#7725#>(+<#7725#> <#7726#>(expt<#7726#> <#7727#>2<#7727#> <#7728#>x)<#7728#> <#7729#>y))<#7729#> 
Then the application <#61796#><#7733#>(poly<#7733#>\ <#7734#>3<#7734#>\ <#7735#>5)<#7735#><#61796#> can be evaluated as follows:
  <#7740#>(poly<#7740#> <#7741#>3<#7741#> <#7742#>5)<#7742#>
<#7743#>=<#7743#> <#7744#>(+<#7744#> <#7745#>(expt<#7745#> <#7746#>2<#7746#> <#7747#>3)<#7747#> <#7748#>5))<#7748#> 
  <#70882#>;; This line is <#61797#><#7749#>(+<#7749#> <#7750#>(expt<#7750#> <#7751#>2<#7751#> <#7752#>x)<#7752#> <#7753#>y)<#7753#><#61797#> where <#61798#><#7754#>x<#7754#><#61798#> was replaced by <#61799#><#7755#>3<#7755#><#61799#> and <#61800#><#7756#>y<#7756#><#61800#> by <#61801#><#7757#>5<#7757#><#61801#> .<#70882#> 
<#7758#>=<#7758#> <#7759#>(+<#7759#> <#7760#>8<#7760#> <#7761#>5)<#7761#> 
<#7762#>=<#7762#> <#7763#>13<#7763#> 
These last two steps follow from plain arithmetic. Third and finally, we need some rules that help us determine the value of <#61802#><#7767#>cond<#7767#>-expression<#61802#>s. These rules are algebraic rules but are not a part of the standard algebra curriculum:
cond_false:
when the first condition is <#61803#><#7769#>false<#7769#><#61803#>:
  <#7773#>(c<#7773#><#7774#>ond<#7774#> 
    <#7775#>[<#7775#><#7776#>false<#7776#> <#7777#>...]<#7777#> 
    <#7778#>[<#7778#><#7779#>exp1<#7779#> <#7780#>exp2]<#7780#> 
    <#7781#>...)<#7781#> 
  <#7787#>=<#7787#> <#7788#>(c<#7788#><#7789#>ond<#7789#>
      <#7790#>; The first line disappeared.<#7790#> 
      <#7791#>[<#7791#><#7792#>exp1<#7792#> <#7793#>exp2]<#7793#> 
      <#7794#>...)<#7794#> 
then the first <#61804#><#7797#>cond<#7797#><#61804#>-line disappears;
cond_true:
when the first condition is <#61805#><#7798#>true<#7798#><#61805#>:
  <#7802#>(c<#7802#><#7803#>ond<#7803#> 
    <#7804#>[<#7804#><#7805#>true<#7805#> <#7806#>exp]<#7806#> 
    <#7807#>...)<#7807#> 
  <#7813#>=<#7813#> <#7814#>exp<#7814#>


the entire <#61806#><#7817#>cond<#7817#>-expression<#61806#>s is replaced by the first answer;
cond_else:
when the only line left is the <#61807#><#7818#>else<#7818#><#61807#>-line:
  <#7822#>(c<#7822#><#7823#>ond<#7823#> 
    <#7824#>[<#7824#><#7825#>else<#7825#> <#7826#>exp]<#7826#><#7827#>)<#7827#> 
  <#7833#>=<#7833#> <#7834#>exp<#7834#>

the <#61808#><#7837#>cond<#7837#>-expression<#61808#>s is replaced by the answer in the <#61809#><#7838#>else<#7838#><#61809#>-clause.
No other rules are needed to understand <#61810#><#7840#>cond<#7840#><#61810#>. Consider the following evaluation:
<#7845#>(c<#7845#><#7846#>ond<#7846#>
  <#7847#>[<#7847#><#7848#>false<#7848#> <#7849#>1]<#7849#> 
  <#7850#>[<#7850#><#7851#>true<#7851#> <#7852#>(+<#7852#> <#7853#>1<#7853#> <#7854#>1)]<#7854#> 
  <#7855#>[<#7855#><#7856#>else<#7856#> <#7857#>3]<#7857#><#7858#>)<#7858#> 
<#7859#>=<#7859#> <#7860#>(c<#7860#><#7861#>ond<#7861#> 
    <#7862#>[<#7862#><#7863#>true<#7863#> <#7864#>(+<#7864#> <#7865#>1<#7865#> <#7866#>1)]<#7866#> 
    <#7867#>[<#7867#><#7868#>else<#7868#> <#7869#>3]<#7869#><#7870#>)<#7870#> 
<#7871#>=<#7871#> <#7872#>(+<#7872#> <#7873#>1<#7873#> <#7874#>1)<#7874#> 
<#7875#>=<#7875#> <#7876#>2<#7876#> 
It first eliminates a <#61811#><#7880#>cond<#7880#><#61811#>-line and then equates the <#61812#><#7881#>cond<#7881#>-expression<#61812#> with~<#61813#><#7882#>(+<#7882#>\ <#7883#>1<#7883#>\ <#7884#>1)<#7884#><#61813#>. The rest is plain arithmetic again. The rules are equations of the form that we use in arithmetic and algebra on a daily basis. Indeed, the same laws apply to this system of equations as to those in mathematics. For example, if <#61814#><#7885#>a<#7885#>\ <#7886#>=<#7886#>\ <#7887#>b<#7887#><#61814#> and <#61815#><#7888#>b<#7888#>\ <#7889#>=<#7889#>\ <#7890#>c<#7890#><#61815#>, then we also know that <#61816#><#7891#>a<#7891#>\ <#7892#>=<#7892#>\ <#7893#>c<#7893#><#61816#>. A consequence is that as we get better at hand-evaluations, we can skip obvious steps and combine several equational inferences into one. Here is one shorter version of the previous evaluation:
  <#7898#>(c<#7898#><#7899#>ond<#7899#>
    <#7900#>[<#7900#><#7901#>false<#7901#> <#7902#>1]<#7902#> 
    <#7903#>[<#7903#><#7904#>true<#7904#> <#7905#>(+<#7905#> <#7906#>1<#7906#> <#7907#>1)]<#7907#> 
    <#7908#>[<#7908#><#7909#>else<#7909#> <#7910#>3]<#7910#><#7911#>)<#7911#> 
<#7912#>=<#7912#> <#7913#>(+<#7913#> <#7914#>1<#7914#> <#7915#>1)<#7915#> 
<#7916#>=<#7916#> <#7917#>2<#7917#> 
Even more importantly, we can replace any expression by its equal in every context---just as in algebra. Here is a another <#61817#><#7921#>cond<#7921#>-expression<#61817#> and its evaluation:
  <#7926#>(c<#7926#><#7927#>ond<#7927#>
    <#7928#>[<#7928#><#72326#>#tex2html_wrap_inline72860#<#72326#> <#7932#>0]<#7932#> 
    <#7933#>[<#7933#><#7934#>else<#7934#> <#7935#>(+<#7935#> <#7936#>1<#7936#> <#7937#>1)]<#7937#><#7938#>)<#7938#> 
  <#7939#>;; The underlined expression is evaluated first. <#7939#> 
<#7940#>=<#7940#> <#7941#>(c<#7941#><#7942#>ond<#7942#> 
    <#7943#>[<#7943#><#7944#>false<#7944#> <#7945#>0]<#7945#> 
    <#7946#>[<#7946#><#7947#>else<#7947#> <#7948#>(+<#7948#> <#7949#>1<#7949#> <#7950#>1)]<#7950#><#7951#>)<#7951#> 
  <#61819#>;; Here <#7952#>cond<#7952#>_<#7953#>false<#7953#> applies. <#61819#> 
<#7954#>=<#7954#> <#7955#>(c<#7955#><#7956#>ond<#7956#> 
    <#7957#>[<#7957#><#7958#>else<#7958#> <#7959#>(+<#7959#> <#7960#>1<#7960#> <#7961#>1)]<#7961#><#7962#>)<#7962#> 
  <#61820#>;; Using <#7963#>cond<#7963#>_<#7964#>else<#7964#>, we now get an arithmetic expression. <#61820#> 
<#7965#>=<#7965#> <#7966#>(+<#7966#> <#7967#>1<#7967#> <#7968#>1)<#7968#> 
<#7969#>=<#7969#> <#7970#>2<#7970#> 
For the first step, we evaluated the nested, underlined expression, which is clearly essential here, because no <#61821#><#7974#>cond<#7974#><#61821#> rule would apply otherwise. Of course, there is nothing unusual about this kind of computing. We have done this many times in algebra and in the first few sections of this book.
<#7977#>Exercise 8.3.1<#7977#> Evaluate the following expressions step by step:
<#7983#>1.<#7983#> <#7984#>(+<#7984#> <#7985#>(*<#7985#> <#7986#>(<#7986#><#7987#>/<#7987#> <#7988#>12<#7988#> <#7989#>8)<#7989#> <#7990#>2/3)<#7990#> 
          <#7991#>(-<#7991#> <#7992#>20<#7992#> <#7993#>(sqrt<#7993#> <#7994#>4)))<#7994#> 
<#7995#>2.<#7995#> <#7996#>(cond<#7996#> 
         <#7997#>[<#7997#><#7998#>(=<#7998#> <#7999#>0<#7999#> <#8000#>0)<#8000#> <#8001#>false<#8001#><#8002#>]<#8002#> 
         <#8003#>[<#8003#><#8004#>(;SPMgt;<#8004#> <#8005#>0<#8005#> <#8006#>1)<#8006#> <#8007#>(symbol=?<#8007#> <#8008#>'<#8008#><#8009#>a<#8009#> <#8010#>'<#8010#><#8011#>a)]<#8011#> 
         <#8012#>[<#8012#><#8013#>else<#8013#> <#8014#>(=<#8014#> <#8015#>(/<#8015#>  <#8016#>1<#8016#> <#8017#>0)<#8017#> <#8018#>9)]<#8018#><#8019#>)<#8019#> 
<#8020#>3.<#8020#> <#8021#>(cond<#8021#> 
         <#8022#>[<#8022#><#8023#>(=<#8023#> <#8024#>2<#8024#> <#8025#>0)<#8025#> <#8026#>false<#8026#><#8027#>]<#8027#> 
         <#8028#>[<#8028#><#8029#>(;SPMgt;<#8029#> <#8030#>2<#8030#> <#8031#>1)<#8031#> <#8032#>(symbol=?<#8032#> <#8033#>'<#8033#><#8034#>a<#8034#> <#8035#>'<#8035#><#8036#>a)]<#8036#> 
         <#8037#>[<#8037#><#8038#>else<#8038#> <#8039#>(=<#8039#> <#8040#>(/<#8040#>  <#8041#>1<#8041#> <#8042#>2)<#8042#> <#8043#>9)]<#8043#><#8044#>)<#8044#> 
external Solution<#61822#><#61822#> <#8053#>Exercise 8.3.2<#8053#> Suppose the <#8055#>Definitions<#8055#> window contains
<#70884#>;; <#61823#><#8060#>f<#8060#> <#8061#>:<#8061#> <#8062#>number<#8062#> <#8063#>number<#8063#> <#8064#><#8064#><#8065#>-;SPMgt;<#8065#><#8066#><#8066#> <#8067#>number<#8067#><#61823#><#70884#>
<#8068#>(d<#8068#><#8069#>efine<#8069#> <#8070#>(f<#8070#> <#8071#>x<#8071#> <#8072#>y)<#8072#> 
  <#8073#>(+<#8073#> <#8074#>(*<#8074#> <#8075#>3<#8075#> <#8076#>x)<#8076#> <#8077#>(*<#8077#> <#8078#>y<#8078#> <#8079#>y)))<#8079#> 
Show how DrScheme evaluates the following expressions, step by step:
<#8087#>1.<#8087#> <#8088#>(+<#8088#> <#8089#>(f<#8089#> <#8090#>1<#8090#> <#8091#>2)<#8091#> <#8092#>(f<#8092#> <#8093#>2<#8093#> <#8094#>1))<#8094#>
<#8095#>2.<#8095#> <#8096#>(f<#8096#> <#8097#>1<#8097#> <#8098#>(*<#8098#> <#8099#>2<#8099#> <#8100#>3))<#8100#> 
<#8101#>3.<#8101#> <#8102#>(f<#8102#> <#8103#>(f<#8103#> <#8104#>1<#8104#> <#8105#>(*<#8105#> <#8106#>2<#8106#> <#8107#>3))<#8107#> <#8108#>19)<#8108#> 
external Solution<#61824#><#61824#>