hw6 - CS 61A Week 6 solutions HOMEWORK 2.74(Insatiable...

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

View Full Document Right Arrow Icon
CS 61A Week 6 solutions HOMEWORK: 2.74 (Insatiable Enterprises): (a) Each division will have a private get-record operation, so each division's package will look like this: (define (install-research-division) ... (define (get-record employee file) ...) ... (put 'get-record 'research get-record) ...) Then we can write a global get-record procedure like this: (define (get-record employee division-file) ((get 'get-record (type-tag division-file)) employee (contents division-file))) It'll be invoked, for example, like this: (get-record '(Alan Perlis) research-personnel-list) For this to work, each division's file must include a type tag specifying which division it is. If, for example, a particular division file is a sequence of records, one per employee, and if the employee name is the CAR of each record, then that division can use ASSOC as its get-record procedure, by saying (put 'get-record 'manufacturing assoc) in its package-installation procedure. (b) The salary field might be in a different place in each division's files, so we have to use the right selector based on the division tag. (define (get-salary record) (apply-generic 'salary record)) Each division's package must include a salary selector, comparable to the magnitude selectors in the complex number example. Why did I use GET directly in (a) but apply-generic in (b)? In the case of get-salary, the argument is a type-tagged datum. But in the case of get-record, there are two arguments, only one of which (the division file) has a type tag. The employee name, I'm assuming, is not tagged. (c) Find an employee in any division
Background image of page 1

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

View Full DocumentRight Arrow Icon
(define (find-employee-record employee divisions) (cond ((null? divisions) (error "No such employee")) ((get-record employee (car divisions))) (else (find-employee-record employee (cdr divisions))))) This uses the feature that a cond clause with only one expression returns the value of that expression if it's not false. (d) To add a new division, you must create a package for the division, make sure the division's personnel file is tagged with the division name, and add the division's file to the list of files used as argument to find-employee-record. 2.75 Message-passing version of make-from-mag-ang : (define (make-from-mag-ang r a) (lambda (mesg) (cond ((eq? mesg 'real-part) (* r (cos a))) ((eq? mesg 'imag-part) (* r (sin a))) ((eq? mesg 'magnitude) r) ((eq? mesg 'angle) a) (else (error "Unknown op -- Polar form number" mesg)) )) ) Note that the formal parameter names X and Y that the book uses in make-from-real-imag (p. 186) are relatively sensible because they are indeed the x and y coordinates of a point in the plane. X and Y are confusing as names for polar coordinates! I used R and A, for Radius and Angle, but MAGNITUDE and ANGLE would be okay, too. I could have used an internal definition, as they do, instead of
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.

This note was uploaded on 11/21/2009 for the course CS 61A taught by Professor Harvey during the Fall '08 term at Berkeley.

Page1 / 9

hw6 - CS 61A Week 6 solutions HOMEWORK 2.74(Insatiable...

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