{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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))))
Image of page 1

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern