Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Write the function's contract, purpose, and header. The contract specifies the function name and what kind of values the function will use as input and output. The purpose is a short statement describing what (not how) the function will calculate. The header is the part of the function definition specifying the function and parameter names. Type these in the definition window. Put the contract and purpose in comments, as in the following examples:
    Code Block
          ;; owe : nat -> num
          ;; Returns how much money you owe for eating the given
          ;; number of pizza slices.
    
          (define (owe slices) ...)
      
    We won't use DrScheme to verify that our contracts are correct, although that is a very useful thing to do. Look for that in COMP 212.
  2. Make some examples of what the function should do. You need to make sure you understand what the function is supposed to do before you define it. When applicable, use some of the example data. We recommend the following form for these examples:
    Code Block
          ;; Examples:
          ;; (owe 0) => 0
          ;; (owe 10) => 15
       
    The first line provides an explanation of the following, making it more readable. (We haven't introduced symbols yet officially -- we will see them in class soon.) The next two lines each give an example, asking DrScheme to compare (with the numeric equality function =) the actual result with the expected result. You should pick enough examples to test the function adequately. We'll say more about that later, but this should include any interesting inputs like zero.
  3. Write the function body. Soon, we will have lots to say about this step. For now, for programs on "unstructured" data, this is very straightforward, because we are typically given an equation like …
    Code Block
          (define (owe slices)
             (* (/ 12 8) slices))
       
    For sets of intervals or other conditional functions, there should be exactly one condition clause per option.
  4. Test the function. i.e., make sure the previous examples really work. If you wrote the examples as suggested above, you can use them to test the function. Add a test section after your function definition, like this:
    Code Block
          "Testing owe:"
          (equal? (owe 0) 0))
          (equal? (owe 10) 15))
       
    All of these tests should return true , assuming that the tests themselves are correct. If your program doesn't work, either fix it now or put that in comments too, to remind yourself to fix it later, and to let your grader/boss/customer/whomever know also. The better your test, the more errors you'll catch sooner and the less time it will take to write a correct function.
HTML
<table border="1" bgcolor="#efefdf" align="center">
HTML
<tr>
HTML
<td>

Using the design recipe, define a function calculating the area of a right isosceles triangle.

HTML
</td>
HTML
</tr>

...

...

</table>

Simple Data: Booleans and Symbols

...

Numbers are old hat, so let's explore Booleans and symbols some.

HTML
<table bgcolor="#efefdf" border="1">

HTML<caption>

Exercises: Using built-in data

HTML
</caption>
HTML
<tbody>
HTML
<tr>
HTML
<td>
HTML
<ol>
HTML
<li>

Try #Try calling some the built-in functions like

Code Block
&lt;<=

,

Code Block
=

,

Code Block
and

...

Code Block
symbol=?

on various data. E e.g., is 17 times 18 bigger
than 256?

HTML
</li>
HTML
<li>

What #What are the contracts of these built-in functions?
*Choose any two, and write down the contract.
*Verify your guess by asking the labby
or by using DrScheme's Help Desk to look at the
manual for the Beginning Student language.

HTML
</li>
HTML
<li>

*Practice writing a function which returns a Boolean:
Following the design recipe,
write

...

which takes in two numbers

...


...

<var>

m

HTML
</var>

and

HTML
<var>

n

HTML
</var>

, and returns true if

HTML
<nobr>
HTML
<var>

m

HTML
</var>

-

HTML
<var>

n

HTML
</var>
HTML
</nobr>

and

HTML
<nobr>
HTML
<var>

n

HTML
</var>

-

HTML
<var>

m

HTML
</var>
HTML
</nobr>

and {{ n - m}} are both
less than 2.
Otherwise, it should return false.
(For learning purposes only, don't use

...

Code Block

     (boolean=? (within-two? 99.8 101) true)
     (boolean=? (within-two? 5 -5)     false)
            
HTML
</li>
HTML
</ol>
HTML
</td>
HTML
</tr>
HTML
<tr>
HTML
<td>

Sample solution

HTML
</td>
HTML
</tr>
HTML
</tbody>

...

...

Other Basics

We assume that you already know how to use email and a Web browser. If you have any trouble, ask a labbie during office hours, or contact the staff of the information desk in Mudd.

...