CS61A_su03_mt2

CS61A_su03_mt2 - cs61a-summer-2003-mt2 University of...

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

View Full Document Right Arrow Icon
University of California, Berkeley - College of Engineering Department of Electrical Engineering and Computer Sciences Instructor: Kurt Meinz Summer 2003 2003-07-25 CS 61A Midterm #2 Personal Information First and Last Name Your Login cs61b-_ _ you attend you attend "All the work is my own. I had no prior knowledge of the exam contents nor will I share the contents with others in CS61a who have not taken it yet. (please sign) Instructions - Partial credit may be given for incomplete / wrong answers, so please write down as much of the solution as you can. -Feel free to use any Scheme function that was described in lecture or sections of the textbook we have read without defining it yourself. Do not use functions or constructs that we have not yet covered. Unless specifically prohibited, you are allowed to use helper functions on any problem. -Please use "true" instead of #t, and "false" instead of #f. We have found that handwritten #t and #f unfortunately look too much alike. -Please write legibly! If we can't read it, we won't grade it! - Feel free to draw funny pictures/messages on the exam. Grading Results Question Max Points Points Earned 1 10 2 10 3 10 4 10 Total 40 cs61a-summer-2003-mt2 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
Question 1a: Deep List Recursion The substitution model of evaluation says that, before a procedure body can be evaluated, any occurrences of the formal parameters in the body must be replaced by the actual argument values in the invocation. We would like to model this behavior by writing a 'substitute' procedure that takes two arguments, a list of parameter-name and actual-argument-value pairs (represented as 2-element lists) and an input list that looks like a Scheme expression. Your procedure should replace every occurrence of a parameter name with its respective value in the target list. Examples: > (substitute '((x 3) (y 4)) '(+ (* x y) (- y z))) (+ (* 3 4) (- 4 z)) > (substitute '((foo (lambda (x) (* x x))) '(foo 4)) ((lambda (x) (* x x ) 4) > (substitute '((z 17) (x (+ y z)) (y 12)) '(* x z)) (* (+ y z) 17) > (substitute '((func (lambda (x) (* x x))) (lst (list 1 2 3))) '(cons (func (car lst)) (map func (cdr lst)))) (cons ((lambda (x) (* x x)) (car (list 1 2 3)))) (map (lambda (x) (* x x)) (cdr (list 1 2 3))))) Hints: 1. We suggest using two recursive processes. One for each pair, and one to go down the list of pairs. 2. Use 'equal?' (define (substitute replace-lst target-lst) cs61a-summer-2003-mt2 2
Background image of page 2
Question 1b: Substitute, continued. The naive substitution procedure that you wrote in Part A would be cooler if it could do repeated substitutions. Please write a new procedure named 'supersub' that takes the same arguments as the usual substitute, but, this time, the parameter-value list may contain more parameters. Your 'supersub' procedure should repeatedly substitute values for parameters until no more substitutions can be made. You may assume that there will be no cycles. > (supersub '((z 17) (x
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
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 Berkeley.

Page1 / 14

CS61A_su03_mt2 - cs61a-summer-2003-mt2 University of...

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

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