hw5 - CS 61A Week 5 solutions HOMEWORK: 2.7 (define...

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

View Full Document Right Arrow Icon
Sheet1 Page 1 CS 61AWeek 5 solutions HOMEWORK: 2.7 (define (upper-bound interval) (cdr interval)) (define (lower-bound interval) (car interval)) 2.8 (define (sub-interval x y) (add-interval x (make-interval (- (upper-bound y)) (- (lower-bound y))))) (define (sub-interval x y) (make-interval (- (lower-bound x) (upper-bound y)) (- (upper-bound x) (lower-bound y)) )) 2.1 (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))))
Background image of page 1

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

View Full DocumentRight Arrow Icon
Sheet1 Page 2 (define (percent i) (* 100 (/ (/ (- (upper-bound i) (lower-bound i)) 2) (/ (+ (lower-bound i) (upper-bound i)) 2)))) (define (percent i) (* 100 (/ (- (upper-bound i) (lower-bound i)) (+ (lower-bound i) (upper-bound i))))) (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.2 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)))) (else (same-parity (cons (car numbers) (cddr numbers)))))) (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
Background image of page 2
Sheet1 Page 3 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))) others))) 2.22. What's wrong with iterative square-list? I've sort of answered this already, in talking about 2.18 (iterative
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/14/2009 for the course PEIS 100 taught by Professor Mckenzie during the Spring '08 term at University of California, Berkeley.

Page1 / 21

hw5 - CS 61A Week 5 solutions HOMEWORK: 2.7 (define...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online