lab2 - CS 61A Week 2 Lab Solutions FIRST LAB: Problem 1: f...

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

View Full Document Right Arrow Icon
Sheet1 Page 1 CS 61AWeek 2Lab Solutions FIRST LAB: Problem 1: fAny 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))))
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/14/2009 for the course PEIS 100 taught by Professor Mckenzie during the Spring '08 term at University of California, Berkeley.

Page1 / 6

lab2 - CS 61A Week 2 Lab Solutions FIRST LAB: Problem 1: f...

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

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