This preview shows pages 1–3. Sign up to view the full content.
CS 61A
Week 2
Lab and Homework Solutions
FIRST LAB:
Problem 1:
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
argument, so we have to define it that way:
(define (f x) (+ x 5))
(f 3) is
8
(define (f x) 'hello)
(f 3) is
hello
(define (f x) (word x x))
(f 3) is
33
Again, these definitions are shorthand for lambda expressions:
(define f (lambda (x) (+ x 5)))
(define f (lambda (x) 'hello))
(define f (lambda (x) (word x x)))
((f)) This expression says, first of all, to compute the subexpression
(f), which invokes f as a procedure with no arguments.
Then, the
result of that invocation must be another procedure, which is
also invoked with no arguments.
So, we have to define f as a
procedure that returns a procedure:
(define (f) (lambda () 'hello))
((f)) is
hello
(define (f) (lambda () (+ 2 3)))
((f)) is
5
Or without the shorthand,
(define f (lambda () (lambda () 'hello)))
(define f (lambda () (lambda () (+ 2 3))))
Alternatively, we can let the procedure f return some procedure
we already know about, supposing that that procedure can be
invoked with no arguments:
(define (f) +)
((f)) is
0
(define f (lambda () +))
As a super tricky solution, for hotshots only, try this:
(define (f) f)
((f)) is
#<procedure f>
(((f))) is.
... ?
(((f)) 3)
Sheesh!
F has to be a function.
When we invoke it with no
arguments, we should get another function (let's call it G).
When we invoke G with no arguments, we get a third function
(call it H).
We have to be able to call H with the argument 3
and get some value.
We could spell this out as a sequence of
definitions like this:
(define (h x) (* x x))
(define (g) h)
(define (f) g)
(((f)) 3) is
9
Alternatively, we can do this all in one:
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document(define (f) (lambda () (lambda (x) (* x x))))
or without the abbreviation:
(define f (lambda () (lambda () (lambda (x) (* x x)))))
By the way, you haven't yet learned the notation for functions that accept
any number of arguments, but if you did know it, you could use
(define (f . args) f)
as the answer to *all* of these problems!
Problem 2:
This is a "do something to every word of a sentence" problem, like
PLSENT 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, ENDSE, etc.,
so it takes the form of a threebranch COND:
(define (substitute sent old new)
(cond ((empty? sent) '())
((equal? (first sent) old)
(se new (substitute (butfirst sent) old new)))
This is the end of the preview. Sign up
to
access the rest of the document.
 Spring '08
 Harvey

Click to edit the document details