Booleans and Relations

#drnseccondbooleans#1808> Consider the following problem statement:
Company XYZ & Co.\ pays all its employees $12 per hour. A typical employee works between 20 and 65 hours per week. Develop a program that determines the wage of an employee from the number of hours of work, <#1810#>if the number is within the proper range<#1810#>.
The italic words highlight the new part (compared to section~#secbasicswordprobs#1812>). They imply that the program must deal with its input in one way if it is in the legitimate range, and in a different way if it is not. In short, just as people need to reason about conditions, programs must compute in a conditional manner. Conditions are nothing new. In mathematics we talk of true and false claims, which are conditions. For example, a number may be equal to, less than, or greater than some other number. If x and y are numbers, we state these three claims about x and y with
  1. x = y: <#1814#>``x is equal to y''<#1814#>;
  2. x ;SPMlt; y: <#1815#>``x is strictly less than y''<#1815#>;
  3. x ;SPMgt; y: <#1816#>``x is strictly greater than y''<#1816#>.
For any specific pair of (real) numbers, exactly one of these claims holds. If x=4 and y=5, the second claim is a true statement, and the others are false. If x=5 and y=4, however, the third claim is true, and the others are false. In general, a claim is true for some values of the variables and false for others. In addition to determining whether an individual claim holds in a given situation, it is sometimes important to determine whether combinations of claims hold. Consider the three claims above, which we can combine in several ways:
  1. #tex2html_wrap_inline72540#
  2. #tex2html_wrap_inline72542#
  3. #tex2html_wrap_inline72544#
The first compound claim is false because no matter what numbers we pick for <#1825#>x<#1825#> and <#1826#>y<#1826#>, two of the three claims are false. The second compound claim, however, always holds no matter what numbers we pick for x and~y. Finally, the third kind of compound claim is the most important of all, because it is true in some cases and false in others. For example, it holds when x = 4, y = 4 and x = 4, y = 5, but is false if x = 5 and y = 3. Like mathematics, Scheme has ``words'' for expressing truth and falsity, for stating individual claims, for combining claims into compound claims, and for expressing that a claim is true or false. The ``word'' for true is <#60543#><#1827#>true<#1827#><#60543#> and the ``word'' for false is <#60544#><#1828#>false<#1828#><#60544#>. If a claim concerns the relationship between two numbers, it can typically be expressed with a <#60545#><#1829#>RELATIONAL OPERATION<#1829#><#60545#>, for example, <#60546#><#1830#>=<#1830#><#60546#>, <#60547#><#1831#>;SPMlt;<#1831#><#60547#>, and <#60548#><#1832#>;SPMgt;<#1832#><#60548#>. Translating the three mathematical claims from above follows our well-known pattern of writing left parenthesis, followed by the operator, its arguments, and a right parenthesis: conditions:
  1. <#60549#><#1834#>(=<#1834#>\ <#1835#>x<#1835#>\ <#1836#>y)<#1836#><#60549#>: <#1837#>``x is equal to y''<#1837#>;
  2. <#60550#><#1838#>(;SPMlt;<#1838#>\ <#1839#>x<#1839#>\ <#1840#>y)<#1840#><#60550#>: <#1841#>``x is strictly less than y''<#1841#>; and
  3. <#60551#><#1842#>(;SPMgt;<#1842#>\ <#1843#>x<#1843#>\ <#1844#>y)<#1844#><#60551#>: <#1845#>``x is strictly greater than y''<#1845#>.
We will also encounter <#60552#><#1847#>;SPMlt;=<#1847#><#60552#> and <#60553#><#1848#>;SPMgt;=<#1848#><#60553#> as relational operators. A Scheme expression that compares numbers has a result just like any other Scheme expression. The result, however, is <#60554#><#1849#>true<#1849#><#60554#> or <#60555#><#1850#>false<#1850#><#60555#>, not a number. That is, when an individual Scheme claim about two numbers is true, it evaluates to <#60556#><#1851#>true<#1851#><#60556#>. For example,
  <#1856#>(;SPMlt;<#1856#> <#1857#>4<#1857#> <#1858#>5)<#1858#> 
<#1859#>=<#1859#> <#1860#>true<#1860#> 
Similarly, a false claim evaluates to <#60557#><#1864#>false<#1864#><#60557#>:
  <#1869#>(=<#1869#> <#1870#>4<#1870#> <#1871#>5)<#1871#> 
<#1872#>=<#1872#> <#1873#>false<#1873#> 
external ~<#1878#>While many algebra texts do not discuss the evaluation of such comparison claims, a thorough understanding of a programming notation requires an understanding of such evaluations.<#1878#> Expressing compound conditions in Scheme is equally natural. Suppose we want to combine <#60558#><#1879#>(=<#1879#>\ <#1880#>x<#1880#>\ <#1881#>y)<#1881#><#60558#> and <#60559#><#1882#>(;SPMlt;<#1882#>\ <#1883#>y<#1883#>\ <#1884#>z)<#1884#><#60559#> so that the compound claim holds if both conditions are true. In Scheme we would write
<#1889#>(and<#1889#> <#1890#>(=<#1890#> <#1891#>x<#1891#> <#1892#>y)<#1892#> <#1893#>(;SPMlt;<#1893#> <#1894#>y<#1894#> <#1895#>z))<#1895#>
to express this relationship. Similarly, if we wanted to formulate a compound condition that is true if (at least) one of the two conditions holds, we would use
<#1903#>(or<#1903#> <#1904#>(=<#1904#> <#1905#>x<#1905#> <#1906#>y)<#1906#> <#1907#>(;SPMlt;<#1907#> <#1908#>y<#1908#> <#1909#>z))<#1909#>
By using <#60560#><#1913#>not<#1913#><#60560#> as follows,
<#1918#>(not<#1918#> <#1919#>(=<#1919#> <#1920#>x<#1920#> <#1921#>y))<#1921#>
we can also state the negation of a claim. Compound conditions, like individual ones, evaluate to <#60564#><#1928#>true<#1928#><#60564#> or <#60565#><#1929#>false<#1929#><#60565#>. Consider the following:
<#1934#>(and<#1934#> <#1935#>(=<#1935#> <#1936#>5<#1936#> <#1937#>5)<#1937#> <#1938#>(;SPMlt;<#1938#> <#1939#>5<#1939#> <#1940#>6))<#1940#>
This compound condition is made up of two individual ones: <#60566#><#1944#>(=<#1944#>\ <#1945#>5<#1945#>\ <#1946#>5)<#1946#><#60566#> and <#60567#><#1947#>(;SPMlt;<#1947#>\ <#1948#>5<#1948#>\ <#1949#>6)<#1949#><#60567#>. Both evaluate to <#60568#><#1950#>true<#1950#><#60568#>, and therefore the evaluation of the <#60569#><#1951#>and<#1951#>-expression<#60569#> continues as follows:
  <#1956#>...<#1956#> 
<#1957#>=<#1957#> <#1958#>(and<#1958#> <#1959#>true<#1959#> <#1960#>true<#1960#><#1961#>)<#1961#> 
If both parts of an <#60570#><#1965#>and<#1965#>-expression<#60570#> are <#60571#><#1966#>true<#1966#><#60571#>, the entire expression evaluates to <#60572#><#1967#>true<#1967#><#60572#>:
   <#1972#>...<#1972#> 
<#1973#>=<#1973#> <#1974#>(and<#1974#> <#1975#>true<#1975#> <#1976#>true<#1976#><#1977#>)<#1977#> 
<#1978#>=<#1978#> <#1979#>true<#1979#> 
In contrast, if one of them is <#60573#><#1983#>false<#1983#><#60573#>, the <#60574#><#1984#>and<#1984#>-expression<#60574#> evaluates to <#60575#><#1985#>false<#1985#><#60575#>:
  <#1990#>(and<#1990#> <#1991#>(=<#1991#> <#1992#>5<#1992#> <#1993#>5)<#1993#> <#1994#>(;SPMlt;<#1994#> <#1995#>5<#1995#> <#1996#>5))<#1996#>
<#1997#>=<#1997#> <#1998#>(and<#1998#> <#1999#>true<#1999#> <#2000#>false<#2000#><#2001#>)<#2001#> 
<#2002#>=<#2002#> <#2003#>false<#2003#> 
The evaluation rules for <#60576#><#2007#>or<#2007#><#60576#> and <#60577#><#2008#>not<#2008#><#60577#> are similarly intuitive. As we get to know Scheme better, we will find other operations for stating conditions about the relationship among different pieces of data.
<#2011#>Exercise 4.1.1<#2011#> What are the results of the following Scheme conditions?
  1. <#60578#><#2014#>(and<#2014#>\ <#2015#>(;SPMgt;<#2015#>\ <#2016#>4<#2016#>\ <#2017#>3)<#2017#>\ <#2018#>(;SPMlt;=<#2018#>\ <#2019#>10<#2019#>\ <#2020#>100))<#2020#><#60578#>
  2. <#60579#><#2021#>(or<#2021#>\ <#2022#>(;SPMgt;<#2022#>\ <#2023#>4<#2023#>\ <#2024#>3)<#2024#>\ <#2025#>(=<#2025#>\ <#2026#>10<#2026#>\ <#2027#>100))<#2027#><#60579#>
  3. <#60580#><#2028#>(not<#2028#>\ <#2029#>(=<#2029#>\ <#2030#>2<#2030#>\ <#2031#>3))<#2031#><#60580#>~ external Solution<#60581#><#60581#>
<#2038#>Exercise 4.1.2<#2038#> What are the results of
  1. <#60582#><#2041#>(;SPMgt;<#2041#>\ <#2042#>x<#2042#>\ <#2043#>3)<#2043#><#60582#>
  2. <#60583#><#2044#>(and<#2044#>\ <#2045#>(;SPMgt;<#2045#>\ <#2046#>4<#2046#>\ <#2047#>x)<#2047#>\ <#2048#>(;SPMgt;<#2048#>\ <#2049#>x<#2049#>\ <#2050#>3))<#2050#><#60583#>
  3. <#60584#><#2051#>(=<#2051#>\ <#2052#>(*<#2052#>\ <#2053#>x<#2053#>\ <#2054#>x)<#2054#>\ <#2055#>x)<#2055#><#60584#>
for (a) <#60585#><#2057#>x<#2057#>\ <#2058#>=<#2058#>\ <#2059#>4<#2059#><#60585#>, (b) <#60586#><#2060#>x<#2060#>\ <#2061#>=<#2061#>\ <#2062#>2<#2062#><#60586#>, and (c) <#60587#><#2063#>x<#2063#>\ <#2064#>=<#2064#>\ <#2065#>7/2<#2065#><#60587#> \ ? external Solution<#60588#><#60588#>