CS61A_fa94_f

# CS61A_fa94_f - CS61A Final-Fall 1994 CS61A Fall 1994 Final...

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

CS61A, Fall 1994 Final Question 1 (5 points): Write a function multicompose that takes as its argument a list of any length, whose elements are one-argument functions. It should return a one-argument function that carries out the composition of all of the given functions, like this: > (define third (multicompose (list first bf bf))) THIRD > (third '(the long and winding road)) AND > ((multicompose (list square square 1+)) 2) 81 > ((multicompose (list square 1+ sqrt 1+)) 8) 16 Your solution must use functional programming style --- no mutation! Question 2 (5 points): Imagine that you are working in a version of Scheme in which the only numbers available are integers. You want to be able to represent amounts of money, such as \$49.95, but you can't use the obvious representation 49.95 because that isn't an integer. You decide to create an abstract data type called a price that has two components, called dollars and cents . You write these constructors and selectors: (define (make-price d c) (+ (* d 100) c)) (define (dollars p) (quotient p 100)) (define (cents p) (remainder p 100)) (a) Write the procedure +price that takes two prices as arguments and returns their sum, as a new price. Respect the data abstraction. (b) Now we want to change the internal representation so that instead of representing a price as a single number of cents, we represent it internally as a list of two numbers, one for the number of dollars and one for the number of leftover cents. Rewrite the constructors and selectors as needed. Don't change the interface to procedures that use the abstraction . Question 3 (5 points): Here is a partial implementation of a telephone system, modeled using objects. Each telephone is an object, and the telephone company's switching equipment is another kind of object. When someone wants to place a call, they ask their telephone to call the number of the other person's phone: CS61A: Final--Fall 1994 1

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

View Full Document
(ask my-phone 'call 6428311) The telephone asks the phone company to connect itself to that number. The result from this request can be one of three things: The word NOT-IN-SERVICE if there is no such telephone number. The word BUSY if the desired telephone is in use already. The word CONNECTED if the connection succeeds. In the third of these cases, the calling phone should note the fact that it's now in use, by setting its BUSY? variable true. The phone company must handle the connect request by finding the desired number in its phones variable, which is an association list of numbers and phones. If the phone exists, the phone company asks it if it's busy. If the phone isn't busy, the connection succeeds. The phone company must ask the called telephone to ring , and must also remember all current connections by maintaining a list of caller-callee pairs in its
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 7

CS61A_fa94_f - CS61A Final-Fall 1994 CS61A Fall 1994 Final...

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

View Full Document
Ask a homework question - tutors are online