Comp 210 Lab 2: Register, DrScheme, Data Definitions

This week's lab primarily covers the use of DrScheme, a software tool for developing Scheme programs.

Index: Register, DrScheme, Simple data definitions


Registering for Comp 210

First, we need to "register" for this course. 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 the questions it asks.

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.

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.


DrScheme

Start DrScheme by either

Either of these should work after you have registered for the course.

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.

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:

(Note: if you use a more advanced language level, DrScheme prints some values differently, e.g., printing lists more succinctly and giving more information about functions.)

To evaluate expressions like (* 3 x), we need to make definitions in the definition window. But when we do, we need to load those definitions into the interaction window.

Let's try some additional definitions.

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

; square of a number
(define square (lambda (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
; square of a number
(define square (lambda (n) (* n n)))
Where the first comment line states that the function square takes a number as input and returns a number as output.


Simple Data Definitions

In class we saw the data definition for a list of numbers:

A list of numbers is

It is good practice always to write your data definitions. In Scheme, the data definitions are not part of your program, but it is good practice to include them as comments in your code, e.g.,

; LoN (list of numbers) is one of
;   1) null
;   2) (cons f r), where f is a number, r is a LoN
In some programming languages, some form of data definitions are part of the program and used by the computer. Regardless of programming language, it will be important to clearly remember your data definition when writing your program.

What are some example lists of numbers?

How would you define a list of symbols?

How would you define a list that can contain both symbols and numbers? Hint: as one option, you can either think of an empty list, or building a list with a symbol, or building a list with a number; as another option, you can think of an empty list, or building a list with a symbol or a number. Do you see the difference? There's nothing too mysterious -- they are equivalent, of course.