Comleted 2.20

This commit is contained in:
Oliver Payne 2021-08-29 22:46:29 +01:00
parent f56d25d722
commit a6b0a5b3b5
5 changed files with 128 additions and 0 deletions

5
2_17.sch Normal file
View File

@ -0,0 +1,5 @@
(define (last-pair l)
(if (null? (cdr l))
(list (car l))
(last-pair (cdr l))))

6
2_18.sch Normal file
View File

@ -0,0 +1,6 @@
(define (reverse l)
(define (reverse-acc l acc)
(if (null? l)
acc
(reverse-acc (cdr l) (cons (car l) acc))))
(reverse-acc l '()))

45
2_19.sch Normal file
View File

@ -0,0 +1,45 @@
;; Counting change
(define (count-change amount)
(cc amount 5))
;;(define (cc amount kinds-of-coins)
;; (cond ((= amount 0) 1)
;; ((or (< amount 0) (= kinds-of-coins 0)) 0)
;; (else (+ (cc amount
;; (- kinds-of-coins 1))
;; (cc (- amount
;; (first-denomination kinds-of-coins))
;; kinds-of-coins)))))
(define (first-denomination kinds-of-coins)
(cond ((= kinds-of-coins 1) 1)
((= kinds-of-coins 2) 5)
((= kinds-of-coins 3) 10)
((= kinds-of-coins 4) 25)
((= kinds-of-coins 5) 50)))
;; EXERCISE 2.19
;; The order of the coin values doesn't matter because we recurse
;; over all coins.
(define us-coins (list 50 25 10 5 1))
(define uk-coins (list 100 50 20 10 5 2 1 0.5))
;: (cc 100 us-coins)
(define (cc amount coin-values)
(cond ((= amount 0) 1)
((or (< amount 0) (no-more? coin-values)) 0)
(else
(+ (cc amount
(except-first-denomination coin-values))
(cc (- amount
(first-denomination coin-values))
coin-values)))))
(define (first-denomination coins) (car coins))
(define (except-first-denomination coins) (cdr coins))
(define (no-more? coins) (null? coins))

8
2_20.sch Normal file
View File

@ -0,0 +1,8 @@
(define (same-parity x . xs)
(define (take-if-par l par)
(cond ((null? l) '())
((= (remainder (car l) 2) par)
(cons (car l) (take-if-par (cdr l) par)))
(else
(take-if-par (cdr l) par))))
(cons x (take-if-par xs (remainder x 2))))

64
2_7.sch Normal file
View File

@ -0,0 +1,64 @@
(define (add-interval x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))))
(define (mul-interval x y)
(let ((p1 (* (lower-bound x) (lower-bound y)))
(p2 (* (lower-bound x) (upper-bound y)))
(p3 (* (upper-bound x) (lower-bound y)))
(p4 (* (upper-bound x) (upper-bound y))))
(make-interval (min p1 p2 p3 p4)
(max p1 p2 p3 p4))))
;;(define (mul-interval x y)
;;(cond ((and (> (lower-bound x) 0)
;;(> (lower-bound y) 0))
;;(make-interval
;;(* (lower-bound x))))))
(define (div-interval x y)
(if (not (= (upper-bound y) (lower-bound y)))
(mul-interval x
(make-interval (/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y))))
(begin
(error "Error: second interval spans 0")
(newline))))
(define (make-interval a b) (cons a b))
(define (lower-bound x) (car x))
(define (upper-bound x) (cdr x))
(define (sub-interval x y)
(make-interval (- (lower-bound x) (upper-bound y))
(- (upper-bound x) (lower-bound y))))
;; 2.12
(define (make-centre-percent c p)
(make-interval (- c (/ (* c p) 100))
(+ c (/ (* c p) 100))))
(define (center i)
(/ (+ (lower-bound i) (upper-bound i)) 2))
(define (width i)
(/ (- (upper-bound i) (lower-bound i)) 2))
(define (percent i)
(* (/ (width i) (center i)) 100))
;; 2.14
;; parallel resistors
(define (par1 r1 r2)
(div-interval (mul-interval r1 r2)
(add-interval r1 r2)))
(define (par2 r1 r2)
(let ((one (make-interval 1 1)))
(div-interval one
(add-interval (div-interval one r1)
(div-interval one r2)))))