# lab7 - CS 61A Week 7 solutions LAB ASSIGNMENT 1 Scheme-1...

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

Page 1 CS 61AWeek 7 solutions LAB ASSIGNMENT: 1. Scheme-1 stuff. (a) ((lambda (x) (+ x 3)) 5) Here's how Scheme-1 handles procedure calls (this is a COND clause inside EVAL-1): ((pair? exp) (apply-1 (eval-1 (car exp)) (map eval-1 (cdr exp)))) The expression we're given is a procedure call, in which the procedure (lambda (x) (+ x 3)) is called with the argument 5. So the COND clause ends up, in effect, doing this: (apply-1 (eval-1 '(lambda (x) (+ x 3))) (map eval-1 '(5))) Both lambda expressions and numbers are self-evaluating in Scheme-1, so after the calls to EVAL-1, we are effectively saying (apply-1 '(lambda (x) (+ x 3)) '(5)) APPLY-1 will substitute 5 for X in the body of the lambda, giving the expression (+ 5 3), and calls EVAL-1 with that expression as argument. This, too, is a procedure call. EVAL-1 calls itself recursively to evaluate the symbol + and the numbers 5 and 3. The numbers are self-evaluating EVAL-1 evaluates symbols by using STk's EVAL, so it gets the primitive addition procedure. Then it calls APPLY-1 with that procedure and the list (5 3) as its arguments. APPLY-1 recognizes that the addition procedure is primitive, so it calls STk's APPLY, which does the actual addition. (b) As another example, here's FILTER: ((lambda (f seq) ((lambda (filter) (filter filter pred seq)) (lambda (filter pred seq) (if (null? seq) '() (if (pred (car seq)) (cons (car seq) (filter filter pred (cdr seq))) (filter filter pred (cdr seq))))))) even? '(5 77 86 42 9 15 8))

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.

{[ snackBarMessage ]}

### Page1 / 10

lab7 - CS 61A Week 7 solutions LAB ASSIGNMENT 1 Scheme-1...

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

View Full Document
Ask a homework question - tutors are online