next up previous
Next: About this document ...

MASSACHVSETTS INSTITVTE OF TECHNOLOGY
Department of Electrical Engineering and Computer Science
6.001--Structure and Interpretation of Computer Programs
Spring Semester, 1999


Recitation - Wednesday, February 3
0. Announcements


1. Rules for Scheme To evaluate an expression, follow these rules:
To apply a procedure to its arguments, evaluate the body of the procedure where each parameter is replaced by the corresponding value. To evaluate a define, evaluate the body of the define, and associate that value with the name listed in the define.

2. Simple Examples To what do the following expressions evaluate (assume they are evaluated in sequence)?
$\Rightarrow$ 7 ;Value: 7
$\Rightarrow$ - ;Value: #[compiled-procedure ..]
$\Rightarrow$ (+ 2 4) ;Value: 6
$\Rightarrow$ (* (- 5 3) (/ 9 3)) ;Value: 6
$\Rightarrow$ (7 - 4) ;Error: The object 7 is not applicable
3. More Examples To what do the following expressions evaluate (assume they are evaluated in sequence)?
$\Rightarrow$ (lambda (x) (* x x)) ;Value: #[compound-procedure ...]
$\Rightarrow$ ((lambda (x) (* x x)) 5) ;Value: 25
$\Rightarrow$ (define double (lambda (x) (* 2 x))) ;Value: "double -> #[compound-procedure ...]
$\Rightarrow$ (double (double 6)) ;Value: 24
$\Rightarrow$ (double double) ;Error: #[compound-procedure] passed to multiply
$\Rightarrow$ (define cube (lambda (x) (*x x x))) ;Value: "cube -> #[compound-procedure ...]
$\Rightarrow$ (cube 3) ;Error: "Unbound variable: *x"
$\Rightarrow$ (define + 3) ;Value: "+ -> 3"
$\Rightarrow$ (define - 6) ;Value: "- -> 6"
$\Rightarrow$ (* + -) ;Value: 18


4. Writing a Procedure Define a procedure called average that computes the average of its two numeric arguments.
(define average (lambda (x y) (/ (+ x y) 2)))


5. Substitution Model Use the substitution model to evaluate the following expression: (average 4 (double 4))
$\Rightarrow$ ([procedure] 4 (double 4)) $\Rightarrow$ $\Rightarrow$ (double 4) $\Rightarrow$ $\Rightarrow$ ([procedure] 4) $\Rightarrow$ $\Rightarrow$ (* 2 4) $\Rightarrow$ ([procedure] 4 8) $\Rightarrow$ (/ (+ 4 8) 2) $\Rightarrow$ ([procedure /] (+ 4 8) 2) $\Rightarrow$ $\Rightarrow$ (+ 4 8) $\Rightarrow$ ([procedure /] 12 2) $\Rightarrow$ 6


6. Subtleties Consider the following two definitions below. How are they similar and how do they differ?
(define plus +)
(define add (lambda (x y) (+ x y)))


7. More Subtleties What do think should be the values of the following expressions? (Note: these are not things you have to memorize.)
$\Rightarrow$ (+ 4) ;Value: 4
$\Rightarrow$ (- 3) ;Value: -3
$\Rightarrow$ (/ 5) ;Value: 0.2
$\Rightarrow$ (/ 60 5 2 3) ;Value: 2
$\Rightarrow$ (+) ;Value: 0
$\Rightarrow$ (*) ;Value: 1
$\Rightarrow$ (-) ;Error: The procedure #[compiled-procedure] requires at least one argment



next up previous
Next: About this document ...
Seth Teller 2004-10-18