Comp 210 Lab 3: Dr. Scheme; Data Definitions

Table of Contents

  • Dr. Scheme, an alternative to donkey
  • Miscellaneous Scheme: quotient, remainder
  • Using Libraries in Dr. Scheme
  • Tips on Running Scheme From Your Home Computer (different page)
  • Documenting Your Code
  • Formatting Your Code
  • Data Definition for List of Bits
  • Dr. Scheme, an alternative to donkey

    Dr. Scheme combines a Scheme interpreter with an environment to write and debug Scheme programs. To start Dr. Scheme, either go to the button labelled Comp210 near the top of your workstation's screen and select ``Dr Scheme'', or type drscheme & (all lower case) and press return. After a quite a few moments (even more than donkey needs) the Dr. Scheme window will pop up.

    Before we forget, there's a one-time task to do: you should set your Language level to "beginner": pull down the Language menu, select ``Configure Language", wait, and where it says "Language V Quasi-R4Rs", click on the triangle and instead select "Beginner". (There is no need to re-start Dr. Scheme.) This setting causes Dr. Scheme to warn you of some common errors that are technically legal, but almost certainly not what you want. And to repeat a warning on the newsgroup: you will get 0 credit for homeworks which use begin or any function with an exclamation point in its name, up until they are introduced in lecture.

    The Dr. Scheme window is divided into two subwindows:

  • The top half is the definition window, where you'll define the Scheme functions for the homework. This window really just is a text editor tailored towards helping you write Scheme.
  • The bottom half, the interaction window, is where the real Scheme interpreter is running. If you want, you can type in Scheme expressions directly into the interaction window, and see how Scheme evaluates them. More often, though, you'll have your code up in the definition window, and then send it down to the interaction window to be evaluated by clicking the Execute button (just above the definition window).
  • Let's try it: In the definition window (top half), type in (+ 1 2 3), then click the Execute button. Sure enough, in the interaction window you see the result 6 printed. Now click back in the definition window, and type something more complicated:

    (define inverse-of
      (lambda (x)
         (/ 1 x)))
    
    What happens when we press Execute this time? Hmmm, once again 6 is printed. Did we do something wrong? No; the 6 is always the result of (+ 1 2 3), which is still in the definitions window. The function inverse-of has been defined, but hasn't been used yet. Go back and add a test in definition window: (inverse-of 37), and execute it.1 (Note that just writing it in the top half doesn't make it known in the bottom window; you must press Execute to do this.)

    When Dr. Scheme, When Donkey?

    Overall, Dr. Scheme is a better environment for writing Scheme code than Donkey is. For the rest of the semester, you can use Dr. Scheme to do the brunt of your work. If you ever are unsure of why your program isn't acting as expected, then you can bring up a Donkey window, cut-and-paste your code from Dr. Scheme into Donkey, and use the Step button.

    As with Donkey, Dr. Scheme's File menu contains options ``Save'', ``Save As'', and ``Print''. (Remember, Scheme files always end in the suffix .ss.)

    Locating and Fixing Mistakes

    If you type in some code that isn't legal Scheme, Dr. Scheme will highlight the problem text as soon as you press the Execute button. Occasionally, it can be useful to instead select ``Syntax Checker'' from the Tools menu. This causes a ``Check Syntax'' button to appear; clicking on the button will have the doctor inspect your code. This approach is handy for finding unmatched parentheses.

    If you have an error, fix it by editing your program, up in the definitions window.

    You can significantly increase your speed (and decrease frustration) by taking a few moments to practice the keyboard shortcuts which work in Donkey and Netscape also work in Dr. Scheme. I particularly recommend C-e (end of line), C-a (start of line), and C-M-f (forward one S-expression).

    Using Libraries in Dr. Scheme

    For Homework 3, you'll need to use the sound library, in order to get use of speak-word, etc. To access the library, go to the Language menu, and select ``Select Library...''. A file-browsing window will appear, asking you to locate the library. It is initially in ~comp210; from there find the directory Projects and press return. Within that directory are only a couple of choices; select one of them.

    To confirm that the desired library is loaded, try one of the promised functions:
    evaluate (word-defined? 'mellifluous).

    quotient and remainder

    Remember the times of elementary school, when the answer to ``20 divided by 3'' was ``6 remainder 2''? Well, you can re-live those good ol' days again:
    (quotient  20 3)  =  6
    (remainder 20 3)  =  2
    
    In addition to nostalgic value, this can be handy if you have 234 cents, and want to know how many whole dollars this is, with how many cents left over.

    Documenting Your Code

    In Scheme, anything between a semicolon ; and the end of the line is a comment. If you want to comment or uncomment a large block of code all at once, first highlight it with the mouse, then go to the Scheme menu, and choose Comment Out or Uncomment appropriately; this will add or remove the semicolons for you.

    This means that to write a function template, you can first write it outside a comment (which will have Dr. Scheme help you indent automatically), and then comment it out all at once, so that the template's ``...'' won't trigger the doctor's error-flagging. To fill in the template, you can copy-and-paste it (see the Edit menu), uncomment it, and replace the template's ``...''s with honest-to-goodness code.

    When writing code in any language, an excellent habit to get into early is to have a comment for each function. The comment should explicitly mention the type and meaning of each of the function's arguments. Writing these comments doesn't take long if you're clear on exactly what the function should do; if you aren't so clear, then writing these comments (before you write the function) helps delineate the problem for you.2

    ;; (fahr c)
    ;; c (number) the temperature in celsius
    ;; Returns a number, the corresponding temperature in Fahrenheit
    ;;
    (define fahr
       (lambda (c)
          (+  (* 9/5 c)  32)))
    

    How much you want to say about how the function works is up to you; for the programs in the first few weeks, the individual functions will be simple enough (once you've written them!) that you'll probably feel they don't require explanations of how they work, which is fine. Your comments can assume that the reader is familiar with Scheme.

    A Note on Formatting

    Formatting your code does matter. While Dr. Scheme may not pay any attention to spaces and returns, human beings do. Adding spaces and carriage returns in the "right" amount makes your code much more understandable to people reading your code. In the short term, human beings are grading your homework. In the long term, real programs are never finished: the author or other programmers go back and modify the code, so it is important and easy to indent your program legibly.

    Some particular points:

  • Preceding every open parenthesis must be a space, an open parenthesis3, or the beginning of a line. No exceptions. ("parenthesis" meaning either round-paren or square-bracket.)
  • Dr Scheme will indent lines for you, but you have to decide where to have one line stop and the next start. If a line is getting too wide, start a new one at a convenient place. For instance:
    ... (cond [(null? argument)  (some-function other-argument)]
              [(complicated-expression (car argument)) (complicated-function argument other-argument)]
              [else (another-complicated-function (cdr argument))])
    
    is easier to read when written:
    ... (cond [(null? argument)
               (some-function other-argument)]
              [(complicated-expression (car argument))
               (complicated-function argument other-argument)]
              [else
               (another-complicated-function (cdr argument))])
    
    Note how you can use square-brackets to help let your eye know where you are in the code.
  • Go ahead and have your last line of code end with a whole passel of piled-up parentheses. Scheme uses parentheses differently than languages like C or Pascal, so it is okay to indent them differently than those other languages.
  • The bottom line is, follow the examples in the book and lecture, and (after a while) your own aesthetic sense, and you should end up with well-formatted code.
    Next page: Data Definition for List of Bits
    1 Is this the same as (inverse-of 37.0)? (back)
    2 I know that many of you will feel this commenting is unnecessary. True, for small toy programs it's no big deal, but it will become a necessary discipline as you start writing larger programs. (back)
    3 Using just the concepts seen so far, when do you ever have two open-parentheses in a row? Is it conceivable to have three in a row? (back)
    Back to Comp 210 Home