Errors

#drnsecbasicseval#792> When we write Scheme programs, we must follow a few carefully designed rules, which are a compromise between a computer's capabilities and human behavior. Fortunately, forming Scheme definitions and expressions is intuitive. Expressions are either <#796#>atomic<#796#>, that is, numbers and variables; or they are <#797#>compound<#797#> expressions, in which case they start with ``('', followed by an operation, some more expressions, and terminated by ``)''. Each expression in a compound expression should be preceded by at least one space; newlines are permissible, and sometimes increase readability. Definitions have the following schematic shape:
<#802#>(d<#802#><#803#>efine<#803#> <#804#>(f<#804#> <#805#>x<#805#> <#806#>...<#806#> <#807#>y)<#807#>
  <#808#>an-expression)<#808#> 
That is, a definition is a sequence of several words and expressions: ``('', the word ``define'', ``('', a non-empty sequence of names separated by spaces, ``)'', an expression, and a closing ``)''. The embedded sequence of names, <#60404#><#812#>f<#812#>\ <#813#>x<#813#>\ <#814#>...<#814#>\ <#815#>y<#815#><#60404#>, introduces the name of the program and the names of its parameters.

<#70649#>Syntax Errors:<#70649#> Not all parenthesized expressions are Scheme expressions. For example, <#60406#><#818#>(10)<#818#><#60406#> is a parenthesized expression, but Scheme does not accept it as a legal Scheme expressions because numbers are not supposed to be included in parentheses. Similarly, a sentence like <#60407#><#819#>(10<#819#>\ <#820#>+<#820#>\ <#821#>20)<#821#><#60407#> is also ill-formed; Scheme's rules demand that the operator is mentioned first. Finally, the following two definitions are not well-formed:

<#826#>(d<#826#><#827#>efine<#827#> <#828#>(P<#828#> <#829#>x)<#829#>
  <#830#>(+<#830#> <#831#>(x)<#831#> <#832#>10))<#832#> 
<#833#>(d<#833#><#834#>efine<#834#> <#835#>(P<#835#> <#836#>x)<#836#> 
  <#837#>x<#837#> <#838#>10)<#838#> 
The first one contains an extra pair of parentheses around the variable <#60408#><#842#>x<#842#><#60408#>, which is not a compound expression; the second contains two atomic expressions, <#60409#><#843#>x<#843#><#60409#> and <#60410#><#844#>10<#844#><#60410#>, instead of one. When we click DrScheme's <#845#>Execute<#845#> button, the programming environment first determines whether the definitions are formed according to Scheme's rules. If some part of the program in the <#846#>Definitions<#846#> window is ill-formed, DrScheme signals a <#60411#><#847#>SYNTAX ERROR<#847#><#60411#> with an appropriate error message and highlights the offending part. Otherwise it permits the user to evaluate expressions in the <#848#>Interactions<#848#> window.
<#851#>Exercise 2.4.1<#851#> #drnexsynerrorexp#854> Evaluate the following sentences in DrScheme, one at a time:
<#859#>(+<#859#> <#860#>(10)<#860#> <#861#>20)<#861#>
<#862#>(10<#862#> <#863#>+<#863#> <#864#>20)<#864#> 
<#865#>(+<#865#> <#866#>+)<#866#> 
Read and understand the error messages.~ external Solution<#60413#><#60413#> <#875#>Exercise 2.4.2<#875#> Enter the following sentences, one by one, into DrScheme's <#877#>Definitions<#877#> window and click <#878#>Execute<#878#>:
<#883#>(d<#883#><#884#>efine<#884#> <#885#>(f<#885#> <#886#>1)<#886#>
  <#887#>(+<#887#> <#888#>x<#888#> <#889#>10))<#889#> 
<#890#>(d<#890#><#891#>efine<#891#> <#892#>(g<#892#> <#893#>x)<#893#> 
  <#894#>+<#894#> <#895#>x<#895#> <#896#>10)<#896#> 
<#897#>(d<#897#><#898#>efine<#898#> <#899#>h(x)<#899#> 
  <#900#>(+<#900#> <#901#>x<#901#> <#902#>10))<#902#> 
Read the error messages, fix the offending definition in an appropriate manner, and repeat until all definitions are legal.~ external Solution<#60414#><#60414#>
<#913#>Run-time Errors:<#913#> The evaluation of Scheme expressions proceeds according to the intuitive laws of algebra and arithmetic. When we encounter new operations, we will extend these laws, first intuitively and then in section~#secsynsem#914> rigorously. For now, it is more important to understand that not all legal Scheme expressions have a result. One obvious example is <#60415#><#915#>(/<#915#>\ <#916#>1<#916#>\ <#917#>0)<#917#><#60415#>. Similarly, if we define
<#922#>(d<#922#><#923#>efine<#923#> <#924#>(f<#924#> <#925#>n)<#925#>
  <#926#>(+<#926#> <#927#>(/<#927#> <#928#>n<#928#> <#929#>3)<#929#> <#930#>2))<#930#> 
we cannot ask DrScheme to evaluate <#60416#><#934#>(f<#934#>\ <#935#>5<#935#>\ <#936#>8)<#936#><#60416#>. When the evaluation of a legal Scheme expression demands a division by zero or similarly nonsensical arithmetic operations, or when a program is applied to the wrong number of inputs, DrScheme stops the evaluation and signals a <#60417#><#937#>RUN-TIME ERROR<#937#><#60417#>. Typically it prints an explanation in the <#938#>Interactions<#938#> window and highlights the faulty expression. The highlighted expression triggered the error signal.
<#941#>Exercise 2.4.3<#941#> Evaluate the following grammatically legal Scheme expressions in DrScheme's <#944#>Interactions<#944#> window:
<#949#>(+<#949#> <#950#>5<#950#> <#951#>(/<#951#> <#952#>1<#952#> <#953#>0))<#953#>
<#954#>(sin<#954#> <#955#>10<#955#> <#956#>20)<#956#> 
<#957#>(somef<#957#> <#958#>10)<#958#> 
Read the error messages.~ external Solution<#60418#><#60418#> <#967#>Exercise 2.4.4<#967#> Enter the following grammatically legal Scheme program into the <#969#>Definitions<#969#> window and click the <#970#>Execute<#970#> button:
<#975#>(d<#975#><#976#>efine<#976#> <#977#>(somef<#977#> <#978#>x)<#978#>
  <#979#>(sin<#979#> <#980#>x<#980#> <#981#>x))<#981#> 
Then, in the <#985#>Interactions<#985#> window, evaluate the expressions:
<#990#>(somef<#990#> <#991#>10<#991#> <#992#>20)<#992#>
<#993#>(somef<#993#> <#994#>10)<#994#> 
and read the error messages. Also observe what DrScheme highlights.~ external Solution<#60419#><#60419#>
<#1005#>Logical Errors:<#1005#> A good programming environment assists the programmer in finding syntax and runtime errors. The exercises in this section illustrate how DrScheme's catches syntax and run-time errors. A programmer, however, can also make <#60420#><#1006#>LOGICAL ERRORS<#1006#><#60420#>. A logical mistakes does not trigger any error messages; instead, the program computes incorrect results. Consider the <#60421#><#1007#>wage<#1007#><#60421#> program from the preceding section. If the programmer had accidentally defined it as
<#1012#>(d<#1012#><#1013#>efine<#1013#> <#1014#>(wage<#1014#> <#1015#>h)<#1015#>
  <#1016#>(+<#1016#> <#1017#>12<#1017#> <#1018#>h))<#1018#> 
the program would still produce a number every time it is used. Indeed, if we evaluate <#60422#><#1022#>(wage<#1022#>\ <#1023#>12/11)<#1023#><#60422#>, we even get the correct result. A programmer can only catch such mistakes by designing programs carefully and systematically.