CSC 153: Computer Science Fundamentals | Grinnell College | Spring, 2005 |
Laboratory Exercise Reading | ||
This lab introduces user-defined procedures (lambda expressions), the Emacs editor for writing Scheme programs, and script for recording sessions.
Although this lab focuses on user-defined procedures, editing, and recording sessions, the lab begins with three useful notes.
Cutting and Pasting Between Windows: In working on a MathLAN 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.
Arithmetic operations as an n-ary operations: In many computer languages, the usual arithmetic operations ( +, −, *, / ) are binary operations −− they apply only to 2 operands. Scheme, however, defines these operators as applying to as many operands as desired.
For example, the volume of a sphere of radius r is given by 4/3 Pi r3. If the radius r is 5, then Scheme allows this to be computed in one statement as follows:
(define volume (* 4/3 3.14159265358979 5 5 5))
The Quote Procedure: Sometimes we want Scheme to print the symbol, not its value. For example, suppose we have defined the symbol pi:
(define pi 3.14159265358979)
Then we might want to print pi, not 3.14159265358979. This is done with the quote procedure:
(quote pi)
In this context, pi is returned (and printed), rather than the value that pi represents.
(quote pi) may be abbreviated 'pi . Try typing this at the keyboard as well.
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 Emacs window. Press the left mouse button on this labeled bar, and move the mouse (keeping the left button depressed). The Emacs window will follow your mouse movements. When the Emacs window is where you want it, release the left mouse button.
While Emacs 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 in Emacs, type <Ctrl/g> to stop the processing of a command.
Work now involves three components:
(load "first-test.ss")More generally, load allows you to specify any file by placing the file name in double quotes.
For much processing in this course, you will move among these windows, copying material from a Firefox window, pasting material into Emacs, editing Emacs, and loading the result into Scheme with the load procedure.
(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).
In Scheme, a lambda expression is called a procedure.
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 would have 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)
Your activity at a workstation may be recorded in a file using the following steps.
script first.session[This indicates that what follows will be recorded in a file called first.session.]
cat first-test.ss
lpr first.session
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/153.sp05/readings/reading-procedures.shtml
created January 22, 1977 last revised January 25, 2005 |
![]() ![]() |
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |