| CSC 153 | Grinnell College | Spring, 2009 |
| Computer Science Fundamentals | ||
Summary: This lab introduces user-defined procedures (lambda expressions), the specification of comments within Scheme programs, and some Scheme predicates — procedures that return Boolean values.
Start DrScheme by clicking on the red, white, and blue lambda symbol on the front panel.
Enter the following into the top definitions window:
(+ 4 5)
(* 4 5)
Click the "Run" button, and observe what is printed in the interactions window.
Add the following definitions to the top definitions.
(define r (+ 2 3))
(define s '(+ 2 3))
(define t (quote (+ 2 3)))
(define u ''(+ 2 3))
(define v (quote (quote (+ 2 3))))
Before your click "Run", move to the interactions window. What happens when you type r, s, t, u, v?
Now click "Run", and describe what happens.
Use the "File" menu to save the material in the definitions window; a common convention is use .ss as the ending for Scheme programs. Following this convention, name this first program file lab2.ss
Shut down DrScheme, and then start it up again. Now load lab2.ss into DrScheme, and execute the program.
Consider the following sequence of definitions computes the volume of a sphere of radius 5:
(define pi 3.14159265358979) (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))
Highlight this code in your browser by moving the mouse to its start, and holding down the left mouse button while moving the mouse to its end. After the section is highlighted, release the left mouse button.
Now paste this highlighted material into the definitions window of DrScheme by moving the mouse to the definition window and clicking the middle mouse button.
Describe the results of this work.
Now click "Run" and retrieve the value computed by typing volume into Scheme.
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?
(define volume (* 4/3 3.14159265358979 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.
Follow a similar pattern using addition + rather than multiplication.
(quote pi)
(quote pi) may be abbreviated 'pi . Try typing this at
the keyboard as well.
Review the results you obtained in part 3 for symbols r, s, t, u, v. In each case, explain why the result was obtained.
((lambda (x) (* x x)) 1)
((lambda (x) (* x x)) 3)
((lambda (x) (* x x))-2)
What happens if you try to apply the procedure to all three values at once?
((lambda (x) (* x x)) 1 3 -2)
In Scheme, a lambda expression is called a procedure.
(define f
(lambda (x) (* x x)))
(f 1)
(f 3)
(f -2)
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~walker/courses/153.sp09/labs/lab-procedures.shtml
|
created 22 January 1977 last revised 20 January 2009 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |