CS61A_sp92_f_sol

# CS61A_sp92_f_sol - CS 60A Spring 1992 Final exam solutions...

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

CS 60A Spring 1992 Final exam solutions 1. Prime factorization. (a) (factors n) --> list of prime factors (define (factors n) (define (helper n s) (cond ((= n 1) '()) ((= (remainder n (head s)) 0) (cons (head s) (helper (/ n (head s)) s)) ) (else (helper n (tail s))) )) (helper n primes)) A lot of people ignored the hint about using the PRIMES stream and checked every possible integer for primality. This is pretty inefficient but not actually wrong. One common mistake was to include factors other than prime factors. Another was to include each prime only once, so (factors 12) would return (2 3) instead of (2 2 3). A third possible mistake is to get confused about streams versus lists; PRIMES is a stream, so you use head/tail to read it, but the value returned by FACTORS is a list, so you construct it with cons, not cons-stream. (b) factor-stream with factors of all the positive integers (define factor-stream (map factors integers)) Scoring: 3 points for (a), 2 for (b). You don't get full credit for (b) unless you use MAP; one of the main things you were supposed to learn in the stream section was to think in terms of these signal processing tools. 2. Abstract data type. I was astonished by the number of questions people asked about this problem during the exam. Many people seem not to know what it means to implement an abstract data type -- it means to write the constructor and selector functions (and mutators if appropriate, but we didn't ask for that in this problem). Many people don't understand how to change the internal representation -- the below-the-line view -- without also changing the way the functions are called (their domains and ranges). So, first of all, here are the domains and ranges of the functions: TIME: domain is two integers; range is a time (the ADT). HOUR: domain is a time; range is an integer. MINUTE: domain is a time; range is an integer. HOUR?: domain is a time; range is true or false. (a) Internal representation of time ADT is a list of two numbers: (define time list) (define hour car)

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

View Full Document
(define minute cadr) (define (hour? t) (= (minute t) 0)) (b) Internal representation of time ADT is an integer number of minutes: (define (time h m) (+ (* h 60) m)) (define (hour t) (div t 60)) (define (minute t) (remainder t 60)) HOUR? is the same as in part a. Scoring: The standard 5-point scale:
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 6

CS61A_sp92_f_sol - CS 60A Spring 1992 Final exam solutions...

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

View Full Document
Ask a homework question - tutors are online