Getting Started – !DrScheme, Design Recipe
Original Author: Dr. John Greiner
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 sign up sheet. Partners are encouraged, but not required, to attend the same lab section.
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 the TAs, labbies, or your college's SCCs how to "map" your Unix account to your personal computer).
For submitting homeworks, see the SVN Turnin page. |
Start DrScheme
|
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
|
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
|
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.
|
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:
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.
;; owe : nat -> num ;; Returns how much money you owe for eating the given ;; number of pizza slices. (define (owe slices) ...)
- 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:
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.
;; Examples: ;; (owe 0) => 0 ;; (owe 10) => 15
- 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 …
For sets of intervals or other conditional functions, there should be exactly one condition clause per option.
(define (owe slices) (* (/ 12 8) slices))
- Test the function. i.e., make sure the previous examples really work. If you wrote the examples as suggested above, you can use them to test the function. Add a test section after your function definition, like this:
All of these tests should return
"Testing owe:" (equal? (owe 0) 0)) (equal? (owe 10) 15))
true
, assuming that the tests themselves are correct. 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. The better your test, the more errors you'll catch sooner and the less time it will take to write a correct function.
Using the design recipe, define a function calculating the area of a right isosceles triangle. |
Simple Data: Booleans and Symbols
We've seen that DrScheme has
- numbers:
17
,-42.89
,3/17
, ... - Booleans:
true
,false
- symbols:
'central-daylight-time
,'galatea2.2
,'mary
, ...
Numbers are old hat, so let's explore Booleans and symbols some.
|
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.
You'll also need to know how to use SVN. On the SVN Turnin Page we have basic instructions for using SVN to turn in your homeworks.
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!
!! Access Permissions
- Set ALLOWTOPICCHANGE = Main.TeachersComp211Group