{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS61A_sp01_mt2

# CS61A_sp01_mt2 - CS61A MT2 Spring 2001 CS61A Spring 2001...

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

CS61A Spring 2001 MT2 Professor Brian Harvey and Professor Dan Garcia Problem #1 Consider the following definitions: > (define c (cons '() '(lets (go) bears))) > (define a (append '() '(lets (go) bears))) > (define 1 (list '() '(lets (go) bears))) (a) What will Scheme print in response to the following expressions? If an expression produces an error message, you may just write "error"; you don't have to provide the exact text of the message. If the value of an expression is a procedure, just write "procedure"; you don't have to show the form in which Scheme prints procedures. Also, draw a box and pointer diagram for the value produced by each expression. > c > a > 1 (b) What will Scheme print in response to the following expressions? Do not draw the box and pointer diagrams for them. > (cdr c) > (cdr a) > (cdr 1) Problem #2 As you already know, lists are made out of pairs. Suppose we want to use an OOP version of pairs to make lists in proper object-oriented style. Louis Reasoner gives the following implementation of OOP pairs: (define-class (pair the-car the-cdr) (method (length) (if (null? the-cdr) 1 CS61A, MT2, Spring 2001 CS61A Spring 2001 MT2 Professor Brian Harvey and Professor Dan Garcia 1

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

View Full Document
(+ 1 (ask the-cdr 'length))))) (a) Happy with his implementation, Louis interacts the following way with his pairs (fill in the blanks): > (define (new-cons a b) (instantiate pair a b)) > (define (new-cdr p) (ask p 'the-cdr)) > (define my-list (new-cons 3 '())) > (ask my-list 'length) ________________________________________________ > (define other-list (new-cdr my-list)) > (ask other-list 'length) ________________________________________________ (b) Reimplement OOP-style lists so that the examples in part (a) return 1 and 0 as they should. You may create or modify classes, methods, and/or procedures as needed. You must use OOP style.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 5

CS61A_sp01_mt2 - CS61A MT2 Spring 2001 CS61A Spring 2001...

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

View Full Document
Ask a homework question - tutors are online