# lab2 - CS 61A Week 2 Lab Solutions Problem 1 is just...

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

CS 61A Week 2 Lab Solutions Problem 1 is just what-will-Scheme-print. Problem 2: This is a "do something to every word of a sentence" problem, like PL-SENT or SQUARES, but with two extra arguments. But it also has a decision to make for each word (is this word equal to the one we're replacing), like the filtering procedures EVENS, ENDS-E, etc., so it takes the form of a three-branch COND: (define (substitute sent old new) (cond ((empty? sent) '()) ((equal? (first sent) old) (se new (substitute (butfirst sent) old new))) (else (se (first sent) (substitute (butfirst sent) old new))))) Problem 3: If (g) is a legal expression, then g takes ZERO arguments. If ((g) 1) has the value 3, then (g) has a PROCEDURE as its value. (If we'd asked for more than one word, you could say "a procedure of one numeric argument that returns a number" or something.) Problem 4: f Any definition at all will do: (define f 'hello) f is hello (define f (+ 2 3)) f is 5 (define (f x) (+ x 7)) f is #<procedure f> (f) This expression says to invoke f as a procedure with no arguments. For that to work, we must DEFINE f as a procedure with no arguments: (define (f) 'hello) (f) is hello (define (f) (+ 2 3)) (f) is 5 Each of these is shorthand for an explicit use of lambda: (define f (lambda () 'hello)) (define f (lambda () (+ 2 3)) (f 3) This expression says to invoke f as a procedure with an

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.

### Page1 / 3

lab2 - CS 61A Week 2 Lab Solutions Problem 1 is just...

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

View Full Document
Ask a homework question - tutors are online