Index:
DrScheme Basics,
Step-by-step Evaluation,
Design Recipe,
Other Basics,
Directions for Unix
The main goals of this lab are to ensure that you can
-
use the basic functionality of DrScheme and
-
write simple programs in a systematic manner.
Outside of lab, don't forget to get a homework partner, if you haven't
already. If you're having trouble getting a partner, try using the course
mailing list. Partners are encouraged, but not required,
to attend the same lab section.
Create your WebCT account
|
Assuming you haven't done so for a previous course,
create a WebCT account for yourself.
In this course, you'll use that for two features:
online quizzes and accessing your grades.
|
DrScheme Basics
Make a new directory (folder), for today's work.
|
On the Owlnet PCs, your "My Documents" folder is the same
location as your "U:" drive, which is the same location as
your Unix login directory.
We will refer to your U: drive, as this is more closely related
to how your computer in your room might be set up (ask
your college's CCA how to "map"
your Unix account into your personal computer).
-
Make a folder in your U: drive called Comp210.
-
Make a sub-folder in
Comp210 called Labs.
-
Make a sub-folder in Labs called Lab01.
-
Save all your Lab01 work to this folder.
For homeworks, make a corresponding
Comp210\Homeworks\HWnn folder.
|
Start DrScheme
-
Start the DrScheme program., which can be found
in the Start menu under
Program\Applications\PLTScheme.
-
If this is the first time you've run DrScheme,
it will ask you your preferred language,
and the Scheme language level; the latter should
be "Beginning Student".
This level makes DrScheme 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 just
a text editor which is smart about indenting Scheme, and such.
The execute button sends the contents of
the definition window down to the interaction window, where they are
actually evaluated.
For home use, you can
download DrScheme.
For our running example, we'll look at a common college problem:
how to divide the cost of a pizza. For simplicity, we'll assume
that a pizza costs $12 and is cut into 8 slices.
Simple example of using DrScheme
-
Type (or cut/paste) the following into definition window,
including:
(+ 2 7)
(* (+ 2 7) 3)
; owe: nat -> num
; Returns how much money you owe for eating the given number of
; slices of pizza.
;
(define (owe slices)
(* (/ 12 8) slices))
Observe that DrScheme helps you with the indentation if you use
the Return key in appropriate places.
It also "bounces" the cursor
to visually match the closing parenthesis with the corresponding
opening parenthesis.
-
We included comments before each function definition,
explaining what it is supposed to do.
DrScheme ignores the comments,
but you, your grader, your boss, etc., can read them. Soon, we'll
provide more guidance on what to say in your comments.
; One form of comments is anything following a semicolon
; up to the end of the line.
#|
Another form of comments is anything between these
two matching markers.
|#
You can also go to the Special menu,
choose Insert comment box.
You are welcome to do this, but one caveat: if DrScheme
saves a file which includes a fancy comment box, the file will not
be saved in plain-text format.
(This is only an issue if you want to open or
print the file with a different program later.)
-
Edit your definitions or comments some.
You can use the mouse, the
arrow keys, backspace, and standard keyboard shortcuts to move the
cursor around.
Also, the PC, Mac, and Unix versions of DrScheme each use
their respective standard keyboard shortcuts.
(You can look in the Edit menu
under Keybindings for the complete list.)
-
Load the definitions into the interaction window by clicking on
the Run button.
If DrScheme detects any syntactic errors, it will give you an
error message in the interactions window and highlight the
error in the definitions window.
-
Use those definitions by typing in the interaction window some
Scheme expressions using those definitions. E.g.,
(owe 5)
(owe 7)
-
Save your work in your
U:\comp210\Labs\Lab01\
directory. Be sure to use a filename that has a
".scm" extension.
You don't need to turn in your lab work.
In fact, you don't have to save it, but we recommend it
so that you can look at it again.
-
Finally, intentionally introduce an error
(or, what you think
will be an error), press Run, and see what error message is
given. We'll go around the room to see what different
error messages people get.
Do the error messages tend to make sense?
|
DrScheme has lots of other features, including a complete manual.
We'll explore more of DrScheme in later labs, and we encourage you
to explore, but there's no need to learn all its features.
Read the
DrScheme Tips and Traps page
for helpful information on common mistakes, error messages, etc.
Step-by-step Evaluation
While DrScheme evaluates our Scheme programs for us, it is also important
for us to understand the process of evaluation. The details of evaluation
will be covered in class. Here in lab, we want to explore two useful tools
to help us:
-
DrScheme's stepping tool that illustrates evaluation.
-
A technique for the programmer to illustrate and check
evaluation "by hand".
Using DrScheme's stepper
-
Place an example use or two of your functions in the definitions
window, after the appropriate definitions. For example,
(owe 10)
-
Click on the Step button.
This brings up a new stepper windows which will show how
DrScheme calculates the answer.
-
Use the stepper's button to look through the evaluation.
At each step, it shows what part of the code needs
to be evaluated next, and what the result is.
|
Illustrating hand-evaluation
|
"Hand-evaluation" is just you doing the
same thing as DrScheme's stepper.
It is useful to convince yourself that you know what
is supposed to happen, independently of having DrScheme help you.
-
Hand-evaluate a few example expressions, and type in each of the
small steps you would make to calculate the result. Type these in
the definitions window after your definitions. For example,
; Hand-evaluation:
(owe 10)
(* (/ 12 8) 10)
(* 3/2 10)
15
This is just high school algebra, successively simplfying
expressions, until you reach a final, simple
value.
-
What do you hope to see, when you Run all these
expressions? What would you see if there were a mistake?
-
While you are doing this, it is often 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.
-
Verify your steps by clicking Run.
-
Once you've used this to verify your steps, put the steps in
comments, rather than deleting them.
(Later, you might change the code, and you want to re-use
these as test cases.)
|
Design Recipe
We will discuss these in class this coming week, but here's a preview.
Programming methodology is a very important component of this course, and
you will be required to follow these ideas, so the earlier you get into the
habit of using them, the better.
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 seem annoying at
first, in the end it will save you lots of errors and grief.
These guidelines will be our design recipes.
Working with unstructured data, like the numbers that we've seen, is
relatively simple. With more complicated data and program styles, we will
add to the following steps.
-
Write the function's contract, purpose, and header.
The contract specifies the function name and what kind of
values the function will use as input and output. The purpose
is a short statement describing what (not how) the function
will calculate. The header is the part of the function
definition specifying the function and parameter names. Type these in
the definition window. Put the contract and purpose in comments, as in
the following examples:
; owe : nat -> num
; Purpose: Returns how much money you owe for eating the given
; number of pizza slices.
(define (owe slices) ...)
or
#|
owe : nat -> num
Purpose: Returns how much money you owe for eating the given
number of pizza slices.
|#
(define (owe slices) ...)
We won't use DrScheme to verify that our contracts are correct, although
that is a very useful thing to do. Look for that in COMP 212.
-
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. When applicable, use some of the example data. We
recommend the following form for these examples:
'owe_examples
(= (owe 0) 0)
(= (owe 10) 15)
The first line provides an explanation of the following, making it more
readable.
(We haven't introduced symbols yet officially -- we will see them
in class soon.)
The next two lines each give an example, asking DrScheme to
compare (with the numeric equality function =) the actual
result with the expected result.
You should pick enough examples to test the function adequately.
We'll say more about that later,
but this should include any interesting inputs like zero.
-
Write the function body.
Soon, we will have lots to say about this step.
For now, for programs on "unstructured" data,
this is very straightforward, because
we are typically given an equation like …
(define (owe slices)
(* (/ 12 8) slices))
For sets of intervals or other conditional functions, there should be
exactly one condition clause per option.
-
Test the function.
I.e., make sure the previous examples really work. If you wrote the tests
as suggested above, you can test them by just executing the program.
If your program 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/whomever know also.
Using the design recipe
|
Follow these steps to define a function calculating
the area of a right isosceles triangle.
|
Other Basics
We assume that you already know how to use email and a Web browser.
If you have any trouble, ask a labbie during office hours,
or contact the staff of the information desk in Mudd.
Always "log out" from your
account when you are done and leaving.
Otherwise, someone can use your account, e.g., to copy or delete
your assignments.
Be sure to save everything before you log out!
Instructions for running DrScheme from your Unix account
For those of you who choose to use DrScheme on the Owlnet Unix machines,
here's a little help in getting started.
"Register" for COMP 210
|
Before you do anything in your Unix account for Comp210,
you should "register" for the course.
This has nothing to do with the registrar.
This will change your programming environment slightly
to access course software more easily.
-
After you have logged in, type at a shell prompt,
register comp210
(followed by the Return key, like any Unix command),
and answer yes to the questions.
-
Logout and login again, so that the changes take affect.
You won't need to do this ever again.
Now you can start DrScheme by typing
drscheme
at a shell prompt.
If this complains about a file not found, then
register didn't work.
Use ~comp210/bin/drscheme instead.
|