# hw4 - CS 61A Week 4 solutions 2.7(define(upper-bound...

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

CS 61A Week 4 solutions 2.7 (define (upper-bound interval) (cdr interval)) (define (lower-bound interval) (car interval)) 2.8 ;; The smallest possible value for A-B is (smallest A)-(largest B). ;; Likewise, the largest value is (largest A)-(smallest B). (define (sub-interval x y) (add-interval x (make-interval (- (upper-bound y)) (- (lower-bound y))))) ;; It would also be okay to replicate the structure of add-interval instead of ;; using add-interval as a subprocedure, although this isn't exactly following ;; Alyssa's method as the problem suggests: (define (sub-interval x y) (make-interval (- (lower-bound x) (upper-bound y)) (- (upper-bound x) (lower-bound y)) )) 2.10 ;; An interval spans zero if its lower bound is negative (or zero) and ;; its upper bound is positive (or zero). ;; It's only the divisor that we have to worry about. (define (div-interval x y) (if (and (<= (lower-bound y) 0) (>= (upper-bound y) 0)) (error "Can't divide by an interval that spans zero.") (mul-interval x (make-interval (/ 1 (upper-bound y)) (/ 1 (lower-bound y)))))) 2.12 (define (make-center-percent c p) (let ((w (* c p 0.01))) (make-interval (- c w) (+ c w)))) ;; We multiply by 0.01 because p percent is a factor of p/100. ;; If you forgot about that part, it's a relatively minor error ;; for computer science purposes, although rather serious in a ;; practical engineering situation! (define (percent i) (* 100 (/ (/ (- (upper-bound i) (lower-bound i)) 2) (/ (+ (lower-bound i) (upper-bound i)) 2))))

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

View Full Document
;; Slightly more efficient percent: (define (percent i) (* 100 (/ (- (upper-bound i) (lower-bound i)) (+ (lower-bound i) (upper-bound i))))) ;; Alternate versions, using the center-width procedures we already have: (define (make-center-percent c p) (make-center-width c (* c p 0.01))) (define (percent i) (* 100 (/ (width i) (center i)))) 2.17 (define (last-pair lst) (if (null? (cdr lst)) lst (last-pair (cdr lst)))) 2.20 The difficulty in writing recursive procedures that take any number of arguments is that you're going to be tempted to make a recursive call with only one argument, namely a list containing some of the original arguments, sort of like this: (define (same-parity . numbers) (cond ((null? (cdr numbers)) numbers) ((equal? (even? (car numbers)) (even? (cadr numbers))) (cons (car numbers) (same-parity (cdr numbers)))) ; WRONG! (else (same-parity (cons (car numbers) (cddr numbers)))))) ; WRONG! (define (even? num) (= (remainder num 2) 0)) Instead, the easiest thing to do is to define a helper procedure that *does* expect a list of numbers as its one argument. An advantage is that we can then separate out the first number, which is always accepted, as a special case: (define (same-parity tester . others) (define (helper numlist) (cond ((null? numlist) nil) ((equal? (even? tester) (even? (car numlist))) (cons (car numlist) (helper (cdr numlist)))) (else (helper (cdr numlist))))) (cons tester (helper others))) Once we know about higher-order functions, there's an even easier solution: (define (same-parity tester . others) (cons tester
(filter (lambda (num) (equal? (even? tester) (even? num)))

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 11

hw4 - CS 61A Week 4 solutions 2.7(define(upper-bound...

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

View Full Document
Ask a homework question - tutors are online