 User-Defined Procedures

## User-Defined Procedures

Goals: This lab introduces user-defined procedures (lambda expressions), the XEmacs editor for writing Scheme programs, and submit for recording sessions.

### User-Defined Procedures

1. The following sequence of definitions computes the volume of a sphere of radius 5:
```
(define pi 3.1415926535)
(define r 5)
(define r-squared (* r r))
(define r-cubed (* r r-squared))
(define pi-r-cubed (* pi r-cubed))
(define volume (* 4/3 pi-r-cubed))
```

Check (by hand or with a calculator) that this sequence produces the correct result.

In the last expression, why are no parentheses used around the number 4/3?

Does the computation still come out correctly if parentheses are used around 4/3? Why or why not?

2. Helpful Hint: In working on an HP workstation, you can select and paste material from one window to another. Through this semester, this can be particularly helpful, when you want to work with material that appears on the lab directions in the Netscape viewer.

To select material from Netscape, move the cursor to the beginning of a section and push down the left mouse button. Then, holding the button down, move the mouse to the end of the section. (The entire section now should be highlighted.) When the desired section is highlighted, stop pressing on the left mouse button -- the section should stay highlighted. Now move the mouse to where you want to paste the material, and click the middle mouse button.

For practice, select and paste the above sequence of define statements into Scheme within the dtterm window.

3. Multiplication * as an n-ary operation: In the above definition of volume, the multiplication operator * is used as a binary operator -- applying only to 2 operands. Scheme, however, defines the multiplication operator * as applying to as many operands as desired. For example, the computation of step 1 be written in one define statements as follows:
```(define volume (* 4/3 3.1415926535 5 5 5))
```
Check the values Scheme returns for each of the following expressions:
```   (* 2)
(* 2 2)
(* 2 2 2)
(* 2 2 2 2)
(* 2 2 2 2 2)
```
What happens if you do not supply any operands?
```   (*)
```
Hypothesize why you get this result.

4. The Quote Procedure: Sometimes we want Scheme to print the symbol, not its value (e.g., pi, not 3.141592). This is done with the quote procedure. To try this out, type
```    (quote pi)
```
(quote pi) may be abbreviated 'pi . Try typing this at the keyboard as well.

### Using XEmacs

So far, you have typed commands directly into the Scheme environment, which immediately evaluated and printed the result. While this use of Scheme is quite straightforward, it has at least two disadvantages. First, all program definitions must be retyped every time a program is to be run, as nothing is saved from one use of Scheme to the next. Second, the results of a computation are not saved, and thus they are difficult to print. This part of the lab addresses both of these issues.

1. Creating New Scheme Files: We will use the XEmacs editor to create a file for a Scheme program. This is done by clicking with the left mouse button on the icon of a paper and pencil at the bottom of the screen. Shortly, a new window will appear for your use in entering your program.

Often, it is convenient to move this window to the right the computer screen. This is accomplished by moving the mouse to the labeled bar emacs:... at the top of the XEmacs window. Press the left mouse button on this labeled bar, and move the mouse (keeping the left button depressed). The XEmacs window will follow your mouse movements. When the XEmacs window is where you want it, release the left mouse button.

While XEmacs is an extremely powerful editor, many common capabilities are highlighted with buttons and menus at the top of window. These menus are analogous to most word processing packages, and thus are not discussed here. Ask the instructor as questions arise. (If something particularly strange seems to be happening, type <Ctrl/g> to stop the processing of a command.)

Type the following Scheme definitions into the XEmacs file.

```
(define pi 3.141596535)
(define q 'quarts)
(define a (sqrt 2))
```
As you are typing, note that when you type a right parenthesis, XEmacs shows you which left parenthesis it matches. This will be particularly helpful when typing longer Scheme programs.

Save the file by clicking on the save button at the top of the XEmacs window. XEmacs then will open a new window, asking you to give a file name for the program. For example, to save your work in a file first-test.ss, you could type this name into the new file-naming window and hit the return key. File first-test.ss now is ready for use within Scheme.

2. To run a program, you will want to open a dtterm window. As in the previous labs, this may be done by clicking on the icon of a computer screen and keyboard. This time, you may want to move the dtterm window to the lower-left of the screen, so you can see much of both the XEmacs and dtterm windows at the same time. (Netscape still will be at the upper-left of the screen.)

Move the mouse to the dtterm window and type scheme to begin running the Scheme environment. Within Scheme, you can use the definitions from a file with the load procedure. Here, you should type

```
```
More generally, load allows you to specify any file by placing the file name in double quotes.

Check that the definitions from the file work as expected by typing

```
pi
q
a
```
3. Editing Existing Scheme Files: For practice, close the XEmacs window using the Exit XEmacs option under the File menu. To open and re-edit first-test.ss again click on the paper and pencil icon to start XEmacs. Then click on the Open button. A new window will appear, showing the names of some files. You may select first-test.ss by clicking on this name and pressing return. (Alternatively, you may type the name first-test.ss into the open file window.)

4. Symbols and Expressions: Type some additional definitions into first-test.ss, save the changes, and reload the file into scheme with the same load command. Note that you do not need to close either the dtterm or the XEmacs window as you work -- you can just move the mouse from window to window as needed.

Modify first-test.ss so that it contains a typographical error. (Remember to save the file by clicking on the save button.) What happens when you try to load this version of the file into scheme?

5. What happens if you place an arithmetic expression into first-test.ss? For example, include the line
```
(+ 2 3)
```
in the file and load it into scheme. Describe what happens.

6. Add the following definitions to first-test.ss:
```
(define r (+ 2 3))
(define s '(+ 2 3))
(define t (quote (+ 2 3)))
(define u ''(+ 2 3))
(define v (quote (quote (+ 2 3))))
```
Load the revised file into Scheme and check the definitions for r, s, t, u, and v. Explain the results that you observe.

### Lambda Expressions

A lambda expression is a way of defining a mathematical function in Scheme. For example, the squaring function could be written:
```    (lambda (x) (* x x))
```
This expression indicates that the input parameter is x and the result of the function will be computed as (* x x).

1. Apply this function to the values 1, 3, -2 by typing:
```    ((lambda (x) (* x x)) 1)
((lambda (x) (* x x)) 3)
((lambda (x) (* x x))-2)
```
What happens if you try to apply the function to all three values at once?
```    ((lambda (x) (* x x)) 1 3 -2)
```
In Scheme, a lambda expression is called a procedure.

2. Naming Procedures: As the previous example shows, lambda expressions operate on the expression that follows them. However, in the example, the lambda expression was defined only for a single expression, and we had to type the lambda expression each time we used it. Since this is inconvenient, we often want to give the procedure a name, which we then can use repeatedly. More formally, we use define to bind a lambda expression to a symbol. If we call the above function f, then the above computations could be done as follows:
```    (define f
(lambda (x) (* x x)))
(f 1)
(f 3)
(f -2)
```

3. Define a procedure f(x) = x² - 3x + 2 and evaluate it for x = 0, 1, 2, 3, 4 .

4. Define a procedure that computes the volume of a sphere, given its radius.

### Recording a Session:

Your activity at a workstation may be recorded in a file using the following steps.

• Within the dtterm window (before running scheme), begin a recording session with the statement
```
submit first.session

```
[This indicates that what follows will be recorded in a file called first.session.]
Be sure the file name used here is different from the file name used previously for your Scheme definitions. Why?
• Print a copy of your Scheme definitions with the command
```
cat first-test.ss
```
• Run scheme as usual, with the scheme command.
• Check the definitions as before.
• Test the procedures you defined by running them with some test data.
• Stop scheme with <Ctrl/d> as before.
• Stop recording by typing <Ctrl/d> one more time.
• Print the record of your session by typing
```
print first.session
```
1. Follow this recording process with a file that contains your work for parts 5, 10, 12, 13, and 14.
Work to be turned in:
• Comments for parts 1 and 9.
• A copy of the printout from the recording session in part 15.

This document is available on the World Wide Web as

```http://www.math.grin.edu/~walker/courses/153.sp00/lab-procedures.html
```

created January 22, 1997
last revised January 7, 2000