week7 - CS 61A LAB ASSIGNMENT 2.62 Week 7 solutions...

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

View Full Document Right Arrow Icon
CS 61A Week 7 solutions LAB ASSIGNMENT: 2.62. Union-set in Theta(n) time. The key is to realize the differences between union-set and intersection-set. The null case for union-set will be different, because if one of the sets is empty, the result must be the other set. In the element comparisons, one element will always be added to the result set. So the expressions with the element will be the same as intersection-set, only with a cons added. Here's the solution: (define (union-set set1 set2) (cond ((null? set1) set2) ((null? set2) set1) (else (let ((x1 (car set1)) (x2 (car set2))) (cond ((= x1 x2) (cons x1 (union-set (cdr set1) (cdr set2)))) ((< x1 x2) (cons x1 (union-set (cdr set1) set2))) ((< x2 x1) (cons x2 (union-set set1 (cdr set2))))))))) Trees on page 156: (define tree1 (adjoin-set 1 (adjoin-set 5 (adjoin-set 11 (adjoin-set 3 (adjoin-set 9 (adjoin-set 7 '()))))))) (define tree2 (adjoin-set 11 (adjoin-set 5 (adjoin-set 9 (adjoin-set 1 (adjoin-set 7 (adjoin-set 3 '()))))))) (define tree3 (adjoin-set 1 (adjoin-set 7 (adjoin-set 11 (adjoin-set 3 (adjoin-set 9 (adjoin-set 5 '()))))))) Other orders are possible; the constraint is that each node must be added before any node below it. So in each case we first adjoin the root node, then adjoin the children of the root, etc. To make sure this is clear, here's an alternative way to create tree1: (define tree1 (adjoin-set 11 (adjoin-set 9
Background image of page 1

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

View Full DocumentRight Arrow Icon
(adjoin-set 5 (adjoin-set 1 (adjoin-set 3 (adjoin-set 7 '()))))))) 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.
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 homework help was uploaded on 04/09/2008 for the course CS 61A taught by Professor Harvey during the Spring '08 term at Berkeley.

Page1 / 11

week7 - CS 61A LAB ASSIGNMENT 2.62 Week 7 solutions...

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