Getting Started –
...
DrRacket, Design Recipe
Original Author: Dr. John Greiner
...
- use the basic functionality of DrScheme DrRacket and
- write simple programs in a systematic manner.
...
DrRacket (aka DrScheme) Basics
The following are instructions for the Ryon Lab's Linux environment:
Via the graphical user interface (GUI):
- Open the folder on your desktop that is labeled with your NetID ("
netID
's Home")
Via a command line:
- Right-click on an open spot in the folder window and select "Create Folder".
- Name the
- Open a terminal (right click anywhere on the desktop and select "Open Terminal").
- Make a new folder (directory) called
Comp211
, for this course. The command is for making a new folder on Linux ismkdir
. - Make a sub-folder in
Comp211
calledLabs
. - "Comp211".
- Double-click the Comp211 folder to open it and inside of it, create another folder (Make a sub-folder in
Labs
) called "Lab00". - Save all your Lab00 work to this folder. For submitting homeworks, see the Owlspace page for Comp 211.
- Start DrSchemeby typing the command
drscheme
.
#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 with List Abbreviations". This language level provides the expected set of primitive operations and enables DrSchemeto reject "legal" Scheme constructions that are not part of our introductory dialect.
The DrSchemewindow 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.
We suggest that you download DrScheme to your home computer/laptop. Versions are available for all major platforms.
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 copy/paste) the following expressions into the interactions window
Code Block (+ 2 7)
Code Block (* (+ 2 7) 3)
- Type (or copy/paste) the following blocks of code into the definition window, including:
Observe that DrSchemehelps 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.Code Block ;; contract: ;; owe: nat -> num ;; purpose: ;; (owe n) returns how much money you owe for eating n slices of pizza. ;; definition: (define (owe slices) (* (/ 12 8) slices))
- Use the definition of
owe
by typing in the interaction window some Scheme expressions using those definitions, e.g.,Code Block (owe 5) (owe 7)
- We included comments before each function definition, explaining what it is supposed to do. DrSchemeignores 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.
You can also go to the Special menu, choose Insert comment box. You are welcome to do this, but one caveat: if DrSchemesaves 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.)Code Block ; One form of comments is anything following a semicolon ; up to the end of the line. ;; However, you'll often see two semicolons starting a comment. #| Another form of comments is anything between these two matching markers. |#
- 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 DrSchemeeach 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 DrSchemedetects any syntactic errors, it will give you an error message in the interactions window and highlight the error in the definitions window.
- Save your work in your
Comp211\Labs\Lab00
directory. Be sure to use a filename that has a.ss
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?
...
- DrRacket by going to the main Linux "Applications" menu and in the "Rice" sub-menu, clicking on "DrRacket".
Via a command line:
- Open a terminal (right click anywhere on the desktop and select "Open Terminal").
- Make a new folder (directory) called
Comp211
, for this course. The command is for making a new folder on Linux ismkdir
. - Make a sub-folder in
Comp211
calledLabs
. - Make a sub-folder in
Labs
calledLab00
. Save all your Lab00 work to this folder. For submitting homeworks, see the Owlspace page for Comp 211. - Start DrRacket by typing the command "
drscheme
" or "drracket
" (no quotes).
If this is the first time you've run DrRacket, it will ask you your preferred language, and the Scheme language level; the latter should be "Beginning Student with List Abbreviations". This language level provides the expected set of primitive operations and enables DrRacket to reject "legal" Scheme constructions that are not part of our introductory dialect. Read the DrRacket Tips and Traps page for helpful information on common mistakes, error messages, etc.
...
The DrRacket 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.
We suggest that you download DrRacket to your home computer/laptop. Versions are available for all major platforms.
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 DrRacket
- Type (or copy/paste) the following expressions into the interactions window
Code Block (+ 2 7)
Code Block (* (+ 2 7) 3)
- Type (or copy/paste) the following blocks of code into the definition window, including:
Observe that DrRacket 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.Code Block ;; contract: ;; owe: nat -> num ;; purpose: ;; (owe n) returns how much money you owe for eating n slices of pizza. ;; definition: (define (owe slices) (* (/ 12 8) slices))
- Use the definition of
owe
by typing in the interaction window some Scheme expressions using those definitions, e.g.,Code Block (owe 5) (owe 7)
- We included comments before each function definition, explaining what it is supposed to do. DrRacket 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.
You can also go to the Special menu, choose Insert comment box. You are welcome to do this, but one caveat: ifCode Block ; One form of comments is anything following a semicolon ; up to the end of the line. ;; However, you'll often see two semicolons starting a comment. #| Another form of comments is anything between these two matching markers. |#
DrRacket
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 DrRacket 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 DrRacket detects any syntactic errors, it will give you an error message in the interactions window and highlight the error in the definitions window.
- Save your work in your
Comp211\Labs\Lab00
directory. Be sure to use a filename that has a.ss
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?
DrRacket has lots of other features, including a complete manual. We'll explore more of DrRacket in later labs, and we encourage you to explore, but there's no need to learn all its features.
Enabling Linux to open .ss files with DrRacket:
- Right-click any .ss file and select "Properties".
- Switch to the "Open With" tab. If DrRacket already appears as a choice, be sure that it is selected, otherwise
- Click the "Add" button.
- In the dialog box that appears, scroll down and select "DrRacket" and click "Add".
- DrRacket should now appear on the list of available programs. Be sure that the radio button for DrRcket is selected.
- Click "Close".
- Double-clicking any .ss file will now open it with DrRacket. You will need to run it once to get the Interactions window to appear or click "Show Interactions Window" from the "View" menu.
...
Step-by-step Evaluation
While DrSchemeevaluates DrRacket 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:
- !DrSchemeDrRacket's stepping tool that illustrates evaluation.
- A technique for the programmer to illustrate and check evaluation "by hand".
Using DrSchemeDrRacket's stepper
- Place an example use or two of your functions in the definitions window, after the appropriate definitions. For example,
Code Block (owe 10)
- Click on the Step button. This brings up a new stepper windows which will show how DrSchemecalculates how DrRacket 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.
...
"Hand-evaluation" is just you doing the same thing as DrSchemeDrRacket's stepper. It is useful to convince yourself that you know what is supposed to happen, independently of having DrSchemehelp DrRacket 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,
This is just high school algebra, successively simplfying expressions, until you reach a final, simple value.Code Block ;; Hand-evaluation: (owe 10) (* (/ 12 8) 10) (* 3/2 10) 15
- 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.)
...
- 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 DrSchemeto DrRacketto verify that our contracts are correct, although that is a very useful thing to do. Look for that in COMP 212.Code Block ;; contract: ;; owe : nat -> num ;; ;; purpose: ;; (owe n) returns how much money you owe for eating n pizza slices. ;; ;; header: ;; (define (owe slices) ...)
- Make some examples of what the function should compute. 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 DrSchemeto DrRacketto 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.Code Block ;; Examples: ;; (owe 0) => 0 ;; (owe 10) => 15
- 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 examples, like this:
All of these tests should pass , assuming that the code forCode Block ;; Tests: (check-expect (owe 0) 0) (check-expect (owe 10) 15)
owe
is correct. The more elaborate your test, the more errors you'll catch sooner and the less time it will take to write a correct function. Note that at this pointowe
has not been written yet. Still we write the test code first! This is one of the key steps in modern software development: test before coding. It is called test-driven development. This is where we deviate from the design recipe describe in HTDP (the textbook). We write the test code first! - 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.Code Block ;; definition: (define (owe slices) (* (/ 12 8) slices))
...
Anchor | ||||
---|---|---|---|---|
|
We've seen that DrScheme DrRacket has
- numbers:
17
,-42.89
,3/17
, ... - Booleans:
true
,false
- symbols:
'central-daylight-time
,'galatea2.2
,'mary
, ...
...
- Try calling some the built-in operations like
<=
,=
,and
,or
,not
,equal?
on various data values, e.g., is 17 times 18 bigger than 256? - What are the contracts of these built-in functions?
*Choose any two, and write down the contract.
*Verify your guess by asking the labby
or by using DrSchemeDrRacket's Help Desk to look at the
manual for the Beginning Student language.
*Practice writing a function which returns a Boolean:
Following the design recipe,
write
which takes in two numbersCode Block within-two?
m
andn
, and returns true if
m - n
andn - m
are both
less than2
.
Otherwise, it should return false.
(For learning purposes only, don't useabs
,
which computes the absolute value. Also, don't use
cond
orif
.)
Your examples/tests can look likeCode Block (check-expect (within-two? 99.8 101) true) (check-expect (within-two? 5 -5) false)
...