Rice University

COMP 210: Principles of Computing and Programming

Lab # 1    Fall 2003

Getting Started: DrScheme, Design Recipe

Index: DrScheme Basics, Step-by-step Evaluation, Design Recipe, Other Basics

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

Outside of lab, don't forget to get a homework partner, if you haven't already. If you're having trouble getting a partner, try using the course newsgroup. Partners are encouraged, but not required, to attend the same lab section.

"Register" for COMP 210

This has nothing to do with the registrar. This will change your programming environment slightly to access course software more easily.

  1. After you have logged in, type at a UNIX prompt,

                register comp210
                
    (followed by the Return key, like any UNIX command), and answer yes to the questions.

  2. Logout and login again, so that the changes take affect.

You won't need to do this ever again.


DrScheme Basics

Make a new directory (folder), for today's work.

This will show you a few UNIX basics you'll need to know, and prepare you for using DrScheme.

  1. From your home directory, type the UNIX command

                ls -l
                
    You should see a directory called comp210, created by the previous step. If you don't, tell one of the lab assistants.

  2. Change directory into the comp210 directory with the UNIX command

                cd comp210
                

  3. Make a directory called Labs by typing

                mkdir Labs
                

  4. Change directory into the Labs directory.

  5. Make an Lab01 directory for this lab.

  6. Change directory into the Lab01 directory.

That's almost all you will need to know about the UNIX operating system for this course. But, if you would like to learn more, you can take a UNIX short course offered by Information Technology. Also, COMP 212's labs will describe more UNIX basics.

Start DrScheme
  1. Enter

                drscheme
                
    at a UNIX prompt. (If this complains about a file not found, then register didn't work. Use ~comp210/bin/drscheme instead.)

  2. If this is the first time you've run DrScheme, it will ask you your preferred language, and the Scheme language level; the latter should be "Beginning Student".

    This level 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.

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 nothing but a text editor which is smart about indenting Scheme, and such. The execute button sends the contents of the definition window down to the interaction window, where they are actually evaluated.

For home use, you can download DrScheme.

Simple example of using DrScheme
  1. Enter (or cut/paste) the following into definition window, including:

                (+ 2 7)
                (* (+ 2 7) 3)
    
                ; owe: nonneg-num -> nonneg-num
                ; Return the amount owed for eating slices slices.
                ;
                (define (owe slices)
                   (* (/ 12 8) slices))
                

    Observe that DrScheme helps you with the indentation if you use the Return key in appropriate places. It also "bounces" the cursor to visually match the closing parenthesis with the corresponding opening parenthesis.

  2. We included comments before each function definition, explaining what it is supposed to do. DrScheme ignores the comments, but you, your grader, your boss, etc., can read them. Soon, we'll provide more guidance on what to say in your comments.

                ; One form of comments is anything following a semicolon
                ; up to the end of the line.
                
                #|
                Another form of comments is anything between these
                two matching markers.
                |#
                
    You can also go to the Special menu, choose Insert comment box. You are welcome to do this, but one caveat: if DrScheme saves a file which includes a fancy comment box, the file will not be saved in plain-text format. (Only an issue if you want to open or print the file with a different program later.)

  3. Edit your definitions or comments some. You can use the mouse, the arrow keys, backspace, and standard keyboard shortcuts to move the cursor around.

    In DrScheme for UNIX, some handy the keyboard shortcuts include the following:

    • Control-p (previous line),
    • Control-n (next line),
    • Control-b (back character),
    • Control-f (forward character),
    • Control-a (start of line), and
    • Control-e (end of line).

    The PC and Mac versions of DrScheme use their respective standard keyboard shortcuts instead. (You can look in the Edit menu under Keybindings for the complete list.)

  4. Load the definitions into the interaction window by clicking on the Execute button.

    If DrScheme detects any syntactic errors, it will give you an error message in the interactions window and highlight the error in the definitions window.

  5. Use those definitions by typing in the interaction window some Scheme expressions using those definitions. E.g.,

                (owe 5)
                (owe 7)
                

  6. Save your work in your ~/comp210/Labs/Lab01/ directory. Be sure to use a filename that has a .ss or .scm extension.

    You don't need to turn in your lab work. In fact, you don't have to save it, but we recommend it so that you can look at it again.

  7. Finally, intentionally introduce an error (or, what you think will be an error), press Execute, and see what error message is given. We'll go around the room to see what different error messages people get. Do the error messages tend to make sense?

DrScheme has lots of other features, including a complete manual. We'll explore more of DrScheme in later labs, and we encourage you to explore, but there's no need to learn all its features. Read the DrScheme Tips and Traps page for helpful information on common mistakes, error messages, etc.


Step-by-step Evaluation

While DrScheme evaluates our Scheme programs for us, it is also important for us to understand the process of evaluation. The details of evaluation will be covered in class. Here in lab, we want to explore two useful tools to help us:

Using DrScheme's stepper
  1. Place an example use or two of your functions in the definitions window, after the appropriate definitions. For example,

                (owe 10)
                

  2. Click on the Step button.

    This brings up a new stepper windows which will show how DrScheme calculates the answer.

  3. Use the stepper's button to look through the evaluation. At each step, it shows what part of the code needs to be evaluated next, and what the result is.

Illustrating hand-evaluation

"Hand-evaluation" is just you doing the same thing as DrScheme's stepper. It is useful to convince yourself that you know what is supposed to happen, independently of having DrScheme help you.

  1. Hand-evaluate a few example expressions, and type in each of the small steps you would make to calculate the result. Type these in the definitions window after your definitions. For example,

                ; Hand-evaluation:
                (owe 10)
                (* (/ 12 8) 10)
                (* 3/2 10)
                15
                
    This is just high school algebra, successively simplfying expressions, until you reach a final, simple value.

    What do you hope to see, when you Execute all these expressions? What would you see if there were a mistake?

    While you are doing this, it is often 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. Alternatively, there are keyboard equivalents listed in this menu.

  2. Verify your steps by clicking Execute.

  3. Once you've used this to verify your steps, put the steps in comments, rather than deleting them. (You'll find that later you might fix a bug, and you want to re-use these as test cases.)


Design Recipe

We will discuss these in class this coming week, but here's a preview. Programming methodology is a very important component of this course, and you will be required to follow these ideas, so the earlier you get into the habit of using them, the better.

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.

Working with unstructured data, like the numbers that we've seen, is relatively simple. With more complicated data and program styles, we will add to the following steps.

  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:

          ; owe : nonneg-num -> nonneg-num
          ; Purpose: owe returns how much money you owe for the given number of
          ; pizza slices.
          (define (owe slices) ...)
          
    or
          #|
            owe : nonneg-num -> nonneg-num
            Purpose: owe returns how much money you owe for 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:

          "owes examples:"
          (= (owe 0)    0)
          (= (owe 10)  15)
          

    The first line provides an explanation of the following, making it more readable. 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 in the class, 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 the following:

          (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 tests as suggested above, you can test them by just executing the program. 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.

Using the design recipe
Follow these steps to define a function calculating the area of a right isosceles triangle.

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. Under UNIX, we recommend using the Mozilla browser, started by the command mozilla. If you have any trouble, ask a labbie during office hours, or contact the staff of the information desk in Mudd. A very brief description of newsgroups follows.

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 ext 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. A common problem is to "lose" a message by reading it and then not marking it as unread. There are many different news-reading programs you can choose from, including most mail programs and web browsers. ( Trouble accessing newsgroup?)

Always "log out" from your UNIX account 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.

 

©2003 Stephen Wong