lab14 - 4.29 Memoizing or not You'd expect a program that...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
CS61A Week 14 solutions LAB: ---- 4.27 Lazy vs. mutation The first time you type COUNT you get 1; the second time you get 2. Why? When you say (define w (id (id 10))) the DEFINE special form handler eval-definition EVALs its second argument (id (id 10)). Given an application, EVAL calls APPLY to invoke ID for the outer invocation, but the inner invocation is providing an argument to a compound procedure, so it's delayed. That's why COUNT is 1 -- the outer call to ID has actually happened, but not the inner one. The value of W is therefore a promise to compute (id 10), since ID returns its argument. When you ask the evaluator to print W, that promise is fulfilled, and so COUNT becomes 2.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 4.29 Memoizing or not You'd expect a program that uses the same argument repeatedly to be most strongly affected. For example, I wrote (define (n-copies n stuff) (if (= n 0) '() (cons stuff (n-copies (- n 1) stuff)))) Then if you use n-copies with something requiring a fair amount of computation, such as (n-copies 6 (factorial 7)) you can see a dramatic difference. About their square/id example, remember to (set! count 0) before each experiment. Then the memoizing version leaves count at 1, whereas the non-memoizing version sets count to 2. [The continuation part of the lab was just try-this.]...
View Full Document

This note was uploaded on 05/12/2010 for the course CS 61A taught by Professor Harvey during the Spring '08 term at Berkeley.

Ask a homework question - tutors are online