Comp 200: Getting Started: DrScheme

Note If using an owlnet unix machine, the following instructions assume that you have successfully register comp200'd, and logged out and back in.

As mentioned, this course uses Scheme for the programming introduction; the program DrScheme lets you type in Scheme expressions, and evaluate them.

Getting started (on Mac or PC)

Start DrScheme by either

DrScheme should be installed on every Mac and PC on campus. If it's not, let me know. You can also install DrScheme on your personal machine by downloading it from the DrScheme Download Page. It's free!!!

WARNING: Unlike owlnet's UNIX filesystem, PC and Mac machines don't backup your files every night (or even allow you to keep them on the machine's local hard drive). If you do your work on a Mac or PC, please be sure that you save a copy to your owlnet filesystem home directory or the hard drive of your personal machine. Floppy disks are prone to failure!!! Keeping multiple backups on different disks is certainly recommendable.

Getting started (on an owlnet Unix terminal)

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 ~/comp200/bin/drscheme in an xterm window.

Now that you've started . . . one more thing

First, from the Language menu, select Configure Language. From the menu that gives you, select Beginner. DrScheme then says to restart the program, i.e., exit DrScheme and then start it again. DrScheme has the notion of "language levels", and the Beginner level should be the most helpful for you currently. It will give you an error for programs that are valid Scheme, but use features that we haven't taught and are assumed to be typos. I.e., it should help you find more of your mistakes.

Experimenting with Scheme

The DrScheme window is divided into two halves. The lower half, or interaction window, is essentially a Scheme calculator. The top half, or definition window is where we enter all of our (define ...) expressions.

Try entering some small Scheme expressions in the interaction window. DrScheme will respond with each expression's value:

To evaluate expressions like (* 3 x), we need to first define the value of the placeholder x.

This fact suggests a way of finding out whether a placeholder has been defined inside DrScheme. E.g., what's the name of a function for negating a boolean? You haven't seen it yet, so try entering negate as your guess. Well, that gives an error, so try entering not. Well, that is defined, so let's see if it's the right thing: try entering (not true). Yep, it responded false. It looks like it's probably the right thing.

Functions, and the definitions window

So far we have been typing everything into the bottom half of DrScheme. The top half (the definitions window) is just a text-editor, we said. So any work which we might want to save in a file, should be typed in the top half. (The bottom half is just for experimentation.)

Let's try a more complicated additional definitions, this time typing into the top half.

Comments

In order to read your definitions more easily, it's generally helpful to put some English comments before each definition. In Scheme, a comment begins with a semicolon and ends with the end of that line, e.g.,

;; Returns the square of a number.
(define (square n) (* n n))
Comments are ignored by the computer -- they are only to help the human reader of the program (which could be you, the programmer, as it can be easy to forget what a program is supposed to do). Even better for this example, because it's more precise is
;; square: number --> number
;; Returns the square of a number.
(define (square n) (* n n))
Where the first comment line states that the function square takes a number as input and returns a number as output.

Testing

It is also important to test your code. Let's test our simple definition of square. In the interaction window, we can try

(square 5)
and it responds with the answer 25. This is a really simple program, but we could still try a couple more examples. With more complicated programs, testing becomes very important.

On most assignments, you will need to turn in your tests, as well as your code. For convenience, we will put these tests together with the code, immediately following the function definition. For example, since we know five squared should give us 25, we'd write in the top half:

; square: number -> number
; returns the square of a number
(define (square n) (* n n))

(square 5) = 25
(square -1) = 1
Can you predict, what prints in the bottom half, when you press the execute button? Can you explain the results?