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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
Evaluating (2 + 1) would return an error, but it is never evaluated.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/23/2010 for the course CS 61A taught by Professor Harvey during the Fall '08 term at University of California, Berkeley.

Page1 / 6

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online