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

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

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


Registering for Comp 210

You should have an Owlnet account already. Freshmen usually receive accounts during O-week. If you do not already have an account, go to the information desk in Mudd.

To do: First, each student needs to "register" for this course. This has nothing to do with university course registration. This will tweak your computing environment to simplify access to the course tools. In one of the xterm windows, type register comp210 (followed by the Return key). Answer y (yes) to any questions it asks.

To do: For most of these changes to have effect, log out and then log in again. The most visible change is that there is now a small menu for Comp 210 on your screen.

If you have a problem registering, you probably have changed your windowing environment substantially from the Owlnet default. Talk to a labby or Mudd consultant after lab.

For the curious... registering is not necessary, but should be done unless you are very familiar with UNIX. This command alters several files, including your .cshrc and .twmrc, adding things to your PATH and your windows setup.


Sign Ups

Outside of lab, don't forget to sign up for therapy sessions and lab sections.


DrScheme

To do: Start DrScheme by either

Either of these should work after you have registered for the course. If you had problems registering, start DrScheme by typing ~comp210/bin/drscheme in an xterm window.

To do: For this first time only, from the Language menu, select Configure Language. From the menu that gives you, select Beginner. When set at Beginner, DrScheme can 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 merely a text editor (which is smart about indenting Scheme, and such).

Simple examples

To do: Enter the following examples from class:

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

     (define (plnt-wgt ew prm prr)
        (* ew (/ prm (* prr prr))))
Remember, definitions go in the top half.

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 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 plnt-wgt.

Really simple editing

To do: We would like to use more mnemonic placeholder names than s and ew, prm, and prr. Wouldn't slices, earth-wgt, plnt-rel-mass, and plnt-rel-radius be better? Edit the above definitions to use these (or similar names). Then reload ("Execute") your changed definitions and see if they still work as before.

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.


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, an first, seem annoying, in the end it will save you lots of errors and grief.

These guidelines will be our "Design Recipe". For now, our recipe consists of the following four steps:

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

    The contract specifies what kind of values the function will use as input and output, e.g.,

         owe : number -> number
    
         plnt-wgt : number number number -> number
         
    This is just notation listing the function name, the kind of input(s) the function expects, and the kind of output it produces.

    The purpose is a short statement describing what (not how) the function will calculate. Typically, it is just a single sentence, e.g.,

         owe returns how much money you owe for the given number of
         pizza slices.
    
         plnt-wgt returns your weight on another planet, given your earth
         weight, the planet's earth-relative mass, and the planet's
         earth-relative radius.
         

    Type these in DrScheme (in the definition window, where we'll put the function definitions) in comments, e.g.,

         ; owe : number -> number
         ; Purpose: owe returns how much money you owe for the given number of
         ; pizza slices.
         
    or
         #|
         plnt-wgt : number number number -> number
    
         Purpose: plnt-wgt returns your weight on another planet, given your earth
         weight, the planet's earth-relative mass, and the planet's
         earth-relative radius.
         |#
         
    These will be ignored by the "little man" inside the computer, and are only used by the humans reading your code (you, your grader, your boss, ...).

    When it is not obvious from the function name, you should also put in comments a more detailed description of what the function computes.

    The header is the part of the function definition specifying the function name and contract, e.g.,

         (define (owe slices) ...)
    
         (define (plnt-wgt earth-wgt plnt-rel-mass plnt-rel-radius) ...)
         

  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.

    Put these in comments also, e.g.,

         #|
         (owe 0) = 0
         (owe 10) = 15
         |#
         

  3. Write the function body.

    E.g.,

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

    For now, this should be very straightforward. As we learn more about programming, we will need more guidelines.

  4. Test the function. Make sure the previous examples really work.

    If it 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/whatever know also.

To do: Follow these rules to define some functions, e.g., f2c, which converts fahrenheit to celsius.


Step-by-step evaluation

DrScheme's stepper

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

     (owe 10)
     (plnt-wgt 100 318 11)
(If you followed the above steps, you should just need to uncomment some code.) 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 that the "little man" or you would make to calculate the result, e.g.,

     (owe 10)
     = (* (/ 12 8 ) 10)
     = (* 3/2 10)
     = 15
Put these with your code too. 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

If you have any trouble, please contact the staff of the information desk in Mudd.

Email

We assume that you already know how to use email.

Netscape

By now, anyone not living in a cave at least knows what the Web is. Netscape (or, more properly, Netscape Navigator) is a window-based program for exploring the Web.

To do: Use Netscape to look at the course's information.

Be sure to read the information on the course pages! Most of the couse information, including assignmnets, will be available only online.

If you find a document of particular interest, you can remember it by creating a "bookmark" for it: pull down your Bookmarks menu, and select Add Bookmark. Any time you want to revisit that page in the future, you can just select it from the Bookmark menu (where it is now listed).

To exit Netscape, select Exit from the File menu.

News

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.)

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.