{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS61A_su95_mt1_sol

# CS61A_su95_mt1_sol - Summer 1995 CS 61A Midterm#1 Solutions...

This preview shows pages 1–3. Sign up to view the full content.

Summer 1995 CS 61A Midterm #1 Solutions Question 1 [5 points total]: Scheme Expressions =============================================== > (define cs61a-is-fun 1) cs61a-is-fun > (define nothing (lambda () (lambda () 0))) nothing > (* (+ 2 5) (+ 3 6)) 63 > (nothing) PROCEDURE Evaluating the nothing procedure (which takes no arguments) returns the result of evaluating (lambda () 0), i.e., a procedure object. > ((nothing)) 0 Applying the procedure object from above simply returns 0. > (((nothing))) ERROR Since ((nothing)) returns 0, we'd be trying to apply 0 as a procedure. > ((lambda (x) (x x)) (lambda (y) (+ 10 20))) 30 The second lambda expression is applied to itself as its argument, i.e., y is bound to the procedure object obtained by evaluating (lambda (y) (+ 10 20)). But y is ignored since it doesn't appear in the body, (+ 10 20). > (if (< 2 1) (+ 6 5) (else (* 4 3))) ERROR Since "else" is not part of the if special form syntax, the Scheme interpreter attempts to find a binding for the name "else" and gets an unbound variable error. > ((lambda (f x) (if (f x) f x)) even? 10) PROCEDURE The procedure denoted by the lambda expression is applied to even? and 10. Since 10 is even, f is returned, and f is bound to the procedure even? > (let ((cs61a-is-fun (+))) (+ cs61a-is-fun 2)) 2 cs61a-is-fun is bound to the result of adding no numbers to 0, and adding this to 2 gives 2. > (if (and (= cs61a-is-fun 1) (> 1000 0)) cs61a-is-fun (2 + 1)) 1 cs61a-is-fun is bound to 1 (we're no longer in the scope of the above let

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Evaluating (2 + 1) would return an error, but it is never evaluated.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 6

CS61A_su95_mt1_sol - Summer 1995 CS 61A Midterm#1 Solutions...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online