Computer Science 61A - Spring 2001 - Harvey - Midterm 2

Computer Science 61A - Spring 2001 - Harvey - Midterm 2 -...

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

View Full Document Right Arrow Icon
CS61A, MT2, Spring 2001 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) file:///C|/Documents%20and%20Settings/Jason%20Raft. ..20Spring%202001%20-%20Harvey%20-%20Midterm%202.htm (1 of 7)1/27/2007 5:39:58 PM
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS61A, MT2, Spring 2001 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 (+ 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. Problem #3
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

Computer Science 61A - Spring 2001 - Harvey - Midterm 2 -...

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

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