# hw4 - ; (iter (cons (square (car things)) answer) (cdr...

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

4 ; #1. ; ;; Exercise 2.7 (define (upper-bound x) (+ (car x) (* (car x) (/ (car (cdr x)) 100)))) ( (define (lower-bound y) (- (car y) (* (car y) (/ (car (cdr y)) 100)))) ( ;; Exercise 2.8 (define (sub-interval x y) (make-interval (- (lower-bound x) (lower-bound y)) (- (upper-bound x) (upper-bound y)))) ;; Exercise 2.10 (define (div-interval x y) (if (< (upper-bound y) (- (upper-bound y) (lower-bound y))) 'error ((mul-interval x (make-interval (/ 1.0 (upper-bound y)) (/ 1.0 (lower-bound y))))))) ;; Exercise 2.12 (define (make-center-percent c w) (make-interval (- c (* c (/ w 100))) (+ c (* c (/ w 100))))) (define (center i) (/ (+ (lower-bound i) (upper-bound i)) 2)) (define (percent i) (* (/ (- (upper-bound i) (lower-bound i)) 2) 100)) ;; Exercise 2.17 (define (last-pair x) (if (equal? (length x) 1) x (last-pair (cdr x)))) ( ;; Exercise 2.22 ; Because here, he displays the cdr before he displays the car. .. ; (iter (cdr things) ; (cons (square (car things)) answer) ;; We can fix it by simply telling it to display the car before the cdr.

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.

Unformatted text preview: ; (iter (cons (square (car things)) answer) (cdr things)) ; ; The cdr is still being displayed before the car is. .. ; ;; Exercise 2.23 (define is-justin? (lambda (x) (for-each (display (equal? 'justin x))))) ( ; #2 (define (substitute lis old new) (cond ((null? lis) '()) ((list? (car lis)) (append (substitute (car lis) old new) (substitute (cdr lis) old new))) ((equal? (car lis) old) (append (list new) (substitute (cdr lis) old new))) (else (append (list (car lis)) (substitute (list (cdr lis)) old new))))) n ; #3 (define (substitute2 lis old new) (cond ((null? lis) '()) ((list? (car lis)) (append (substitute2 (car lis) old new) (substitute2 (cdr lis) old new))) (else (append (list (substitute-h (list (car lis)) old new)) (substitute-h (list (cdr lis)) old new))))) (define (substitute-h lis old new) (cond ((null? old) lis) ((equal? (car lis) (car old)) (car new)) (else (substitute-h lis (cdr old) (cdr new)))))...
View Full Document

## hw4 - ; (iter (cons (square (car things)) answer) (cdr...

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

View Full Document
Ask a homework question - tutors are online