<#70686#>;; <#60589#><#2077#>is-5?<#2077#> <#2078#>:<#2078#> <#2079#>number<#2079#> <#2080#><#2080#><#2081#>-;SPMgt;<#2081#><#2082#><#2082#> <#2083#>boolean<#2083#><#60589#><#70686#> <#70687#>;; to determine whether <#60590#><#2084#>n<#2084#><#60590#> is equal to <#60591#><#2085#>5<#2085#><#60591#><#70687#> <#2086#>(d<#2086#><#2087#>efine<#2087#> <#2088#>(is-5?<#2088#> <#2089#>n)<#2089#> <#2090#>(=<#2090#> <#2091#>n<#2091#> <#2092#>5))<#2092#>The function produces <#60592#><#2096#>true<#2096#><#60592#> if, and only if, its input is equal to <#60593#><#2097#>5<#2097#><#60593#>. Its contract contains one novel element: the word <#60594#><#2098#>boolean<#2098#><#60594#>. Just like <#60595#><#2099#>number<#2099#><#60595#>, <#60596#><#2100#>boolean<#2100#><#60596#> represents a class of values that is built into Scheme. Unlike <#60597#><#2101#>number<#2101#><#60597#>, <#60598#><#2102#>boolean<#2102#><#60598#> consists of just two values: <#60599#><#2103#>true<#2103#><#60599#> and <#60600#><#2104#>false<#2104#><#60600#>. Here is a slightly more interesting function with a Boolean output:
<#70688#>;; <#60601#><#2109#>is-between-5-6?<#2109#> <#2110#>:<#2110#> <#2111#>number<#2111#> <#2112#><#2112#><#2113#>-;SPMgt;<#2113#><#2114#><#2114#> <#2115#>boolean<#2115#><#60601#><#70688#> <#70689#>;; to determine whether <#60602#><#2116#>n<#2116#><#60602#> is between 5 and 6 (exclusive)<#70689#> <#2117#>(d<#2117#><#2118#>efine<#2118#> <#2119#>(is-between-5-6?<#2119#> <#2120#>n)<#2120#> <#2121#>(and<#2121#> <#2122#>(;SPMlt;<#2122#> <#2123#>5<#2123#> <#2124#>n)<#2124#> <#2125#>(;SPMlt;<#2125#> <#2126#>n<#2126#> <#2127#>6)))<#2127#>It consumes a number and produces <#60603#><#2131#>true<#2131#><#60603#> if the number is between, but does not include, <#60604#><#2132#>5<#2132#><#60604#> and <#60605#><#2133#>6<#2133#><#60605#>. One good way to understand the function is to say that it describes the following interval on the number line:
<#72378#>
<#2147#>Boundaries<#2147#>:\ An interval boundary marked with ``('' or ``)''
is excluded from the interval; an interval boundary marked with ``['' or
``]'' is included.~<#60614#><#60614#>
The following third function from numbers to Boolean values represents the
most complicated form of interval:
<#72379#> <#72380#> <#72381#> <#72382#> <#72383#> <#72384#>
is a claim concerning some unknown number x. For x = -1 the claim holds:
For x = 1, it doesn't because
and 4 is <#2345#>not<#2345#> equal to 0. A number for which the claim holds is
called a <#2346#>solution<#2346#> to the equation.
We can use Scheme to formulate equational conditions as a function. If someone
then claims to have a solution, we can use the function to test whether the
proposed solution is, in fact, a solution. Our running example corresponds
to the function
<#2493#>Testing<#2493#>:\ Writing tests as claims is good practice, though we need
to know more about equality to develop good automatic tests. To do so, we
resume the discussion of equality and testing in
section~#secequaltest#2494><#70692#>;; <#60615#><#2154#>is-between-5-6-or-over-10?<#2154#> <#2155#>:<#2155#> <#2156#>number<#2156#> <#2157#><#2157#><#2158#>-;SPMgt;<#2158#><#2159#><#2159#> <#2160#>boolean<#2160#><#60615#><#70692#>
<#70693#>;; to determine whether <#60616#><#2161#>n<#2161#><#60616#> is between 5 and 6 (exclusive) <#70693#>
<#70694#>;; or larger than or equal to <#60617#><#2162#>10<#2162#><#60617#><#70694#>
<#2163#>(d<#2163#><#2164#>efine<#2164#> <#2165#>(is-between-5-6-or-over-10?<#2165#> <#2166#>n)<#2166#>
<#2167#>(or<#2167#> <#2168#>(is-between-5-6?<#2168#> <#2169#>n)<#2169#> <#2170#>(;SPMgt;=<#2170#> <#2171#>n<#2171#> <#2172#>10)))<#2172#>
The function returns <#60618#><#2176#>true<#2176#><#60618#> for two portions of the number line:
<#2199#>Exercise 4.2.1<#2199#>
Solution<#60679#><#60679#>
<#2283#>Exercise 4.2.2<#2283#>
<#2290#>1.<#2290#> <#2291#>(defin<#2291#><#2292#>e<#2292#> <#2293#>(in-interval-1?<#2293#> <#2294#>x)<#2294#>
<#2295#>(and<#2295#> <#2296#>(;SPMlt;<#2296#> <#2297#>-3<#2297#> <#2298#>x)<#2298#> <#2299#>(;SPMlt;<#2299#> <#2300#>x<#2300#> <#2301#>0)))<#2301#>
<#2302#>2.<#2302#> <#2303#>(defin<#2303#><#2304#>e<#2304#> <#2305#>(in-interval-2?<#2305#> <#2306#>x)<#2306#>
<#2307#>(or<#2307#> <#2308#>(;SPMlt;<#2308#> <#2309#>x<#2309#> <#2310#>1)<#2310#> <#2311#>(;SPMgt;<#2311#> <#2312#>x<#2312#> <#2313#>2)))<#2313#>
<#2314#>3.<#2314#> <#2315#>(defin<#2315#><#2316#>e<#2316#> <#2317#>(in-interval-3?<#2317#> <#2318#>x)<#2318#>
<#2319#>(not<#2319#> <#2320#>(and<#2320#> <#2321#>(;SPMlt;=<#2321#> <#2322#>1<#2322#> <#2323#>x)<#2323#> <#2324#>(;SPMlt;=<#2324#> <#2325#>x<#2325#> <#2326#>5))))<#2326#>
Also formulate contracts and purpose statements for the four functions.
Evaluate the following expressions by hand:
Show the important steps. Use the pictures to check your
results.~ Solution<#60683#><#60683#>
<#2343#>Exercise 4.2.3<#2343#>
<#70710#>;; <#60684#><#2351#>equation1<#2351#> <#2352#>:<#2352#> <#2353#>number<#2353#> <#2354#><#2354#><#2355#>-;SPMgt;<#2355#><#2356#><#2356#> <#2357#>boolean<#2357#><#60684#><#70710#>
<#70711#>;; to determine whether <#60685#><#2358#>x<#2358#><#60685#> is a solution for
When we apply <#60686#><#2377#>equation1<#2377#><#60686#> to some number, we get <#60687#><#2378#>true<#2378#><#60687#> or
<#60688#><#2379#>false<#2379#><#60688#>:
<#2384#>(equation1<#2384#> <#2385#>-1)<#2385#>
<#2386#>=<#2386#> <#2387#>true<#2387#>
and
<#2395#>(equation1<#2395#> <#2396#>+1)<#2396#>
<#2397#>=<#2397#> <#2398#>false<#2398#>
Translate the following equations into Scheme functions:
Determine whether <#60689#><#2404#>10<#2404#><#60689#>, <#60690#><#2405#>12<#2405#><#60690#>, or <#60691#><#2406#>14<#2406#><#60691#> are solutions of
these equations. Solution<#60692#><#60692#>
<#2412#>Exercise 4.2.4<#2412#>
<#2424#>(<#2424#><#70713#><#2425#>Fahrenheit<#2425#><#60694#><#2426#><#2426#><#2427#>-;SPMgt;<#2427#><#2428#><#2428#><#60694#><#2429#>Celsius<#2429#><#70713#> <#2430#>32)<#2430#>
<#2431#>=<#2431#>
<#2432#>0<#2432#>
and
<#2440#>(<#2440#><#70714#><#2441#>Fahrenheit<#2441#><#60695#><#2442#><#2442#><#2443#>-;SPMgt;<#2443#><#2444#><#2444#><#60695#><#2445#>Celsius<#2445#><#70714#> <#2446#>212)<#2446#>
<#2447#>=<#2447#>
<#2448#>100<#2448#>
After clicking the <#2452#>Execute<#2452#> button we can compare the numbers to
the left and right of <#60696#><#2453#>=<#2453#><#60696#>. If they are equal, we know our function
works.
As our results become more and more complex, comparing values becomes more
and more tedious. Using <#60697#><#2454#>=<#2454#><#60697#>, we can instead translate these
equations into claims:
<#2459#>(=<#2459#> <#2460#>(<#2460#><#70715#><#2461#>Fahrenheit<#2461#><#60698#><#2462#><#2462#><#2463#>-;SPMgt;<#2463#><#2464#><#2464#><#60698#><#2465#>Celsius<#2465#><#70715#> <#2466#>32)<#2466#>
<#2467#>0)<#2467#>
and
<#2475#>(=<#2475#> <#2476#>(<#2476#><#70716#><#2477#>Fahrenheit<#2477#><#60699#><#2478#><#2478#><#2479#>-;SPMgt;<#2479#><#2480#><#2480#><#60699#><#2481#>Celsius<#2481#><#70716#> <#2482#>212)<#2482#>
<#2483#>100)<#2483#>
Now, if all claims evaluate to <#60700#><#2487#>true<#2487#><#60700#>, we know that our function
works for the specified examples. If we see a <#60701#><#2488#>false<#2488#><#60701#> anywhere,
something is still wrong.
Reformulate the test cases for exercises~#exf2c#2489>