Comp 210 Lab 1: Getting Started: DrScheme, Design Recipe

Index: Sign Ups, DrScheme, Design Recipe, Step-by-step evaluation, Other basics, Logging out

The main goals of this lab are to ensure that you can


Sign Ups

Outside of lab, don't forget to get a homework partner (if you want to) and sign up for a lab section.


DrScheme

To do: Start DrScheme by typing ~comp210/bin/drscheme in an xterm window (followed by the Return key, like any UNIX command). (There's supposed to be a shortcut, but right now it's broken.)

For home use, you can download DrScheme.

The DrScheme window is divided into two halves. The lower half, or interaction window, is the Scheme "calculator". The top half, or definition window is merely a text editor (which is smart about indenting Scheme, and such).

To do: Verify that the interaction window says "Beginning Student". If it does not, from the Language menu, select Configure Language. From the menu that gives you, select Beginning Student. This makes DrScheme catch more errors for you: namely, errors which are technically legal Scheme, but are probably not what you really want early in the course.

Simple examples of using DrScheme

To do: Enter the following examples from class into the definition window:

     (define (owe slices)
        (* (/ 12 8) slices))

     (define (real-tax ww)
        (cond
           [(<= ww 500)                   (* (/ 15 100) ww)]
           [(and (< 500 ww) (<= ww 1500)) (* (/ 28 100) ww)]
           [(< 1500 ww)                   (* (/ 33 100) ww)]))
(Square brackets are equivalent to parentheses. Aligning the result expressions is visually handy, but not necessary.)

DrScheme helps you with the indentation if you use the Return key in appropriate places. Observe that DrScheme "bounces" the cursor to visually match the closing parenthesis with the corresponding opening parenthesis. To edit, you can use the mouse, arrow keys, and backspace as you would expect. There are also key strokes that you can learn, such as Control-p, Control-n, Control-b, Control-f, Control-a, and Control-e for moving the cursor around. Some people prefer using the mouse, while some people prefer using the keyboard.

To do: Now we want to use those definitions. To do so, we first need to "load" those definitions into the calculator part of DrScheme by clicking on the "Execute" button. Now, in the bottom half of the window, type in some Scheme expressions, including some using owe and real-tax.


Design Recipe

When writing programs, there are lots of things we need to think about. It helps if we have some guidelines to follow that remind us to do these things in the proper order. While following some strict rules can seem annoying at first, in the end it will save you lots of errors and grief. These guidelines will be our design recipes.

Unstructured data

Working with unstructured data, like integers or booleans, is relatively simple.

  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 so that Scheme will ignore them (but, of course, you, your grader, your boss, ... will read them). E.g.,

         ; owe : nonneg-num -> nonneg-num
         ; Purpose: owe returns how much money you owe for the given number of
         ; pizza slices.
         (define (owe slices) ...)
         
    or
         #|
         real-tax : nonneg-num -> nonneg-num
    
         Purpose: real-tax returns the tax you owe on a given weekly wage,
         using a simplified graduated income tax.
         |#
         (define (real-tax weekly-wages) ...)
         

  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.

    Put these in comments also, e.g.,

         #|
         (owe 0)  = 0
         (owe 10) = 15
         |#
         
    Using the equal sign here will be convenient later.

  3. Write the function body.

    For programs on unstructured data, this is very straightforward, because we are typically given an equation, e.g.,

         (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. Make sure the previous examples really work.

    If you wrote the tests as suggested above, you can test them by uncommenting them, and executing the program again. You'll see the results of the tests together with the expected answers. 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.

To do: Follow these steps to define a function calculating the area of a circle, as discussed in class.

Structured data (specifically, natural numbers)

Most of the time, we'll use various kinds of structured data. So far, we've seen only one kind, natural numbers. Soon, we'll see other examples and also tools for creating your own. When writing programs, we want to take advantage of whatever structure we can, so we can expand our design recipe.

  1. Write the appropriate data definition(s).

    A natnum is one of

  2. Create example data following the data definition.

    We're all familiar with the natural numbers, so we can skip this for now.

  3. Write the template for the appropriate data definition.

    The template is code (with holes) which echoes the structure of the data. It is the blueprint for any function that takes that kind of data as input.

    For natural numbers, the template is

         (define (natnum-func n)
            (cond
               [(zero? n) ...]
               [(pos? n)  ...(natnum-func (sub1 n))...]))
         
    Observe that it has one condition clause per data definition clause and recursive calls corresponding to the recursion in the data definition We will discuss templates in much more detail later.

  4. Write the function's contract, purpose, and header.

  5. Write the function body, following the structure of the template.

    Copy the template, renaming it and its recursive calls, and fill in the blanks. We will discuss strategies for doing this in more detail later.

  6. Test the function.

To do: Follow these steps to define a function calculating the sum of the numbers between the input n and zero, i.e., n+(n-1)+...+1+0.


Step-by-step evaluation

DrScheme's stepper

To do: Place a couple example uses of your functions in the definitions section, after the appropriate definitions. For example,

     (owe 10)
     (factorial 3)
To see how DrScheme calculates the answers, press the Step button. The new stepper window will show you what part of the code needs to be evaluated next, and what the result is. The stepper's buttons let you look at each step of the evaluation.

Hand-evaluation

You should be able to do by hand what the DrScheme stepper does.

To do: Hand-evaluate some example expressions. Type in each of the small steps you would make to calculate the result, e.g.,

     (owe 10)
     = (* (/ 12 8) 10)
     = (* 3/2 10)
     = 15
Separating each of the steps with an equal sign is a neat little trick to make things look nice. You can verify your steps clicking "Execute" -- you should see the same answer multiple times, once for each step. (Once you've used this to verify your steps, put it in comments, rather than deleting it.)

While you are doing this, it is helpful to copy the previous step, and edit this copy for the current step. This can save you some typing, and it helps eliminate mistakes. You'll want the "Copy" and "Paste" features in the "Edit" menu. Or, note that there are keyboard equivalents listed in this menu.

Try some other examples, possibly with some other simple definitions.


Other basics

These things probably won't be covered in lab. Make sure you're familiar with

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

Newsgroups are electronic bulletin boards where everybody can post and read articles on certain topics. There is a newsgroup specifically for this course, rice.owlnews.comp210. You should read the newsgroup every day, as helpful hints and clarifications are posted there regularly. Also, if you have a question relating to the course, post it to the newsgroup. Similarly if you have an answer for somebody else's question!

In newsgroups, the idea is that once you've read a message, you probably don't ever want to see it again. So your newsreader "marks" an article as read, and next time you read news it only presents you with unmarked articles. (You also have options such as unmarking read articles or looking at all articles.) Most newsgroup-reading programs also group articles by "thread", i.e., grouping articles with any replies.

There are many different news-reading programs you can choose from. Two leading contenders are Netscape and Pine, each of which makes reading news analagous to their way of reading email.


Logging out

Always "log out" when you are done and leaving. Otherwise, someone can use your account, e.g., to copy or delete your assignments. One way is to type logout in the "console window" which is labeled. You do not need to exit any other programs first, but be sure to save any important changes first.