CS61A_su03_mt2

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

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

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

View Full Document
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
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

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.

## 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
Ask a homework question - tutors are online