[previous] [up] [next]     [contents]
Next: Word Problems- No Notes Up: NumbersExpressions, Simple Programs Previous: Numbers and Arithmetic

Variables and Programs

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 

Compare this pattern to the area-of-disk program:
(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)

because no arguments have been specified. (If DrScheme accepts this definition, double-check that the current language level is Beginning Student; see Preparing DrScheme).

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)

when you type ENTER, DrScheme automatically inserts some space at the beginning of the next line. Whenever the indentation of a line is incorrect, typing TAB with the caret anywhere in the line re-indents the line. Multiple lines can be reformatted by selecting the lines and typing TAB. DrScheme's Scheme|Indent All menu item reformats everything in the program window.

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

The name a-b does not mean a minus b; it's just a name. Even *3.14 is a name. Suppose that you forget the space between * and 3.14 when defining area-of-disk:
(define (area-of-disk r)
  (*3.14 (* r r))) 

When you test this definition of area-of-disk, DrScheme reports that the name *3.14 is undefined.

Avoid the following characters in variable names:

"   '   `   ,   #   ;   |   \ 

Since quotes cannot be used in names, do not attempt to use x' as ``x prime.'' The pairs of bracket characters [ ] and { } are also special: they act just like parenthesis pairs ( ).

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.


[previous] [up] [next]     [contents]
Next: Word Problems- No Notes Up: NumbersExpressions, Simple Programs Previous: Numbers and Arithmetic

PLT