{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# notes14s - 6 8 10 6 8 2 Write a function an-atom-of that...

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

CS61A Notes 14 – Your Father’s Worst Nightmare [Solutions v1.0] Of Pills And Other Euphoric Items QUESTIONS: What is printed at each line? 1. > (define x (+ 2 3)) > x ==> 5 > (define y ((lambda(a) a) (* 3 4))) > y ==> 12 > (define z ((lambda(b) (+ b 10)) y)) > z ==> 22 Note that, without mutation or assignment, lazy evaluation works just like normal STk. 2. > (define count 0) > (define (foo x y) (x y)) > (define z (foo (lambda(a) (set! count a) (* a a)) (begin (set! count (+ 1 count)) count))) > count ==> 0 > z ==> infinite loop (doh!) 3. > (define count 0) > (define (incr!) (set! count (+ count 1))) > (define (foo x) (let ((y (begin (incr!) count))) (if (<= count 1) (foo y) x) > (foo 10) ==> infinite loop Nondeterministic and Indecisive QUESTIONS 1. Suppose we type the following into the amb evaluator: > (* 2 (if (amb #t #f #t) (amb 3 4) 5)) What are all possible answers we can get?

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 6, 8, 10, 6, 8 2. Write a function an-atom-of that dispenses the atomic elements of a deep list (not including empty lists). For example, > (an-atom-of ‘((a) ((b (c))))) ==> a > try-again ==> b (define (an-atom-of ls) (cond ((null? ls) (amb)) ((atom? ls) ls) (else (amb (an-atom-of (car ls)) (an-atom-of (cdr ls)))))) Chung Wu; CS61A, Spring 2004 1 3. Use an-atom-of to write deep-member? . (define (deep-member? X ls) (let ((maybe-x (an-atom-of ls))) (require (equal? x maybe-x)) #t)) 4. Fill in the blanks: > (define (choose-member L R) (cond ((null? R) (amb)) ((= (car L) (car R)) (car L)) (else (amb (choose-member L (cdr R)) (choose-member (cdr L) R))))) > (choose-member ‘(1 2 3) ‘(4 2 3)) 3 > try-again 2 > try-again 2 Chung Wu; CS61A, Spring 2004 2...
View Full Document

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern