Type program definitions in DrScheme's top area, the definitions window, like this:
After the definition is complete, click on the Execute button. DrScheme creates a new prompt in the bottom window and starts evaluating expressions from the definitions window. If there are no errors in the definitions window, then the defined program can be tested the interactions window at the prompt. If there are errors in the definitions window, an error message is printed in the interactions window and the offending expression is highlighted with pink in the definitions window. After fixing an error in the definitions window, click on Execute again.
After your program has been executed, evaluate test expressions in the interactions window, e.g., (area-of-disk 3). Test expressions can also appear in the definitions window. In this case, when the Execute button is clicked, the value of each test expression is displayed in the interactions window.
If the definitions window is modified and you type an expression into the interactions window without clicking Execute, DrScheme warns you that Execute is needed. If you do not click Execute, tests evaluated in the interactions window use programs from the previous Execute.
Don't forget to save your work. When the definitions window is modified, a Save button appears in the top left corner of DrScheme's window. Click on the button to save your definitions to a file, and use the filename extension ``.scm'' for definition files.
The Shape of Definitions
The location of parentheses in a definition must match the following
pattern exactly:
start definition v start program name and input names v v end input names v v v (define (f arg ...) expression) ^ end definition
(define (area-of-disk r) (* 3.14 (* r r)))
In area-of-disk, the expression is (* 3.14 (* r r)). No extra parentheses are added to this expression.
If the parentheses for define are not correct, DrScheme reports the error ``Malformed define''. When this error occurs, check that a single set of parentheses surrounds the program name and argument list, and that a single body expression is provided (perhaps with its own set of parentheses, but with no extra parentheses).
The following is not a legal definition in the
Beginning Student language level:
(define (f) expr)
Formatting Definitions
DrScheme helps you format your definitions in a standard, easy-to-read
way. After typing the first line of a definition:
(define (area-of-disk r)
After a while, you will become used to the standard way of formatting definitions and expressions. When ENTER or TAB does not produce the indentation you expect, check the expression to make sure that parentheses are inserted at the right places. Strange indentation is a sign that your expression has the wrong shape.
Variable Names
Unlike most programming languages, program and variable names in
Scheme can contain almost any character. For example, the following
are legal names:
a-b in->ft my-2nd-program positive? !@$%^&/*~program
(define (area-of-disk r) (*3.14 (* r r)))
Avoid the following characters in variable names:
" ' ` , # ; | \
Names are case-sensitive, i.e., an upper-case letter is treated different from a lower-case letter in a name. For example, the name area-of-disk is different from the name Area-Of-Disk.
Interactions Window Tips
When you click the Execute button, the interactions window is cleared, but your old test expressions are not lost. In the interactions window, typing ESC-P copies the most recent test expression to the current prompt. (``ESC-P'' means ``push the ESC key, let go, and then push the p key''.) Typing ESC-P multiple times produces older and older expressions from the history of interaction expressions, while ESC-N produces newer expressions. Alternatively, place the insertion caret at the end of any old expression in the interactions window and type ENTER to copy the expression down to the current prompt. Of course, cut and paste work as usual.
Exercise Notes
Exercise 2.2.5 Scheme does not have a built-in square operator, but you can define one.