SICP習題解答2 7 2 16

2021-06-09 00:08:37 字數 1342 閱讀 9586

#lang racket

; exercise 2.7

(define (make-interval a b) (cons a b))

(define (upper-bound interval) (max (car interval) (cdr interval)))

(define (lower-bound interval) (min (car interval) (cdr interval)))

; exercise 2.8

(define (sub-interval x y)

(make-interval

(- (lower-bound x) (upper-bound y))

(- (upper-bound x) (lower-bound y))))

; exercise 2.10

(define (div-interval x y)

(if (<= (* (lower-bound y) (upper-bound y)) 0)

-1(mul-interval x

(make-interval (/ 1.0 (upper-bound y))

(/ 1.0 (lower-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))))

; exercise 2.12

(define (make-center-percent c p)

(make-interval (* c (- 1. p))

(* c (+ 1. p))))

(define (percent interval)

(let ((a (upper-bound interval))

(b (lower-bound interval)))

(/ (- a b) (+ a b))))

(define (center interval)

(/ (+ (lower-bound interval) (upper-bound interval)) 2))

; exercise 2.15

;; 正確

SICP習題解答1 1 1 8

lang racket exercise 1.1 10 5 3 4 9 1 6 2 2 4 4 6 define a 3 define b a 1 a b a b a b if and b a b a b ba cond a 4 6 b 4 6 7 a else 25 2 if b a b a co...

SICP習題解答2 17 2 23

lang racket exercise 2.17 define last pair l cond null?l null null?cdr l l else last pair cdr l last pair list 23 72 149 34 last pair 1 last pair exer...

SICP習題1 6的解答

sicp就是名著 structure and interpretation of computer programs 著名的巫師書 wizard book 和紫書 purple book 雖說英文原版可以從網上合法 中文版翻譯也還不錯。閒話少敘,下面我們研究一下書中 1.1.7節的練習題1.6。原題...