This week's lab primarily covers the use of DrScheme, a software tool for developing Scheme programs.
Index: Register, DrScheme, Simple data definitions
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
Start DrScheme by either
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:
This is also an example that you can simply type in an identifier to see if it's used. 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 #t). Yep, it responded #f. It looks like it's probably the right thing.
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.
Note again that DrScheme visually matches parentheses to help you make sure that you typed everything correctly. As an additional check, select the Check Syntax button. This checks the entire definition window for syntax errors, and annotates your code, e.g., highlighting identifiers with color.
To use your definition, select the Execute button. This essentially starts DrScheme afresh, knowing only the definitions currently in the definition window. Now enter (* 3 x) in the interaction window -- it responds with 15.
Let's try some additional definitions.
Note again the parenthesis matching. Also, move the cursor onto each of the opening and closing parentheses and note how DrScheme highlights the appropriate section of code.
Load the definition by selecting the Execute button. Enter (square 5) (in the interaction window) -- it responds with 25, of course.
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.
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 LoNIn 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.