1.3 较大两个数之和
1 (define (MaxSum x y z) 2 (+ (cond ((or (> x y) (> x z)) x) 3 (else 0)) 4 (cond ((or (> y x) (> y z)) y) 5 (else 0)) 6 (cond ((or (> z x) (> z y)) z) 7 (else 0)))) 8 (MaxSum 5 3 10)
牛顿迭代
1 (define (Abs x) 2 (cond ((> x 0) x) 3 ((= x 0) 0) 4 ((< x 0) (- x)))) 5 (define (Square x) (* x x)) 6 (define (Average x y) 7 (/ (+ x y) 2)) 8 (define (Improve guess x) 9 (Average guess (/ x guess))) 10 (define (GoodEnough? guess x) 11 (< (Abs (- (Square guess) x)) 0.001)) 12 (define (SqrtIter guess x) 13 (if (GoodEnough? guess x) 14 guess 15 (SqrtIter (Improve guess x) 16 x))) 17 (SqrtIter 1 2)
一个表的最后一个表
1 (define (last-pair lst) 2 (if (<= (length lst) 1) 3 lst 4 (last-pair (cdr lst))))
集合操作
1 (define (element-of-set? x set) 2 (cond ((null? set) false) 3 ((equal? x (car set)) true) 4 (else (element-of-set? x (cdr set))))) 5 6 (define (adjoin-set x set) 7 (if (element-of-set? x set) 8 set 9 (cons x set))) 10 11 (define (intersection-set set1 set2) 12 (cond ((or (null? set1) (null? set2)) '()) 13 ((element-of-set? (car set1) set2) 14 (cons (car set1) 15 (intersection-set (cdr set1) set2))) 16 (else (intersection-set (cdr set1) set2)))) 17 18 (define (union-set set1 set2) 19 (cond ((null? set1) set2) 20 ((null? set2) set1) 21 ((element-of-set? (car set1) set2) 22 (union-set (cdr set1) set2)) 23 (else (cons (car set1) (union-set (cdr set1) set2))))) 24 25 26 (define set1 (list 1 2 3 4)) 27 (define set2 (list 3 4 5 6)) 28 (element-of-set? 3 set1) 29 (element-of-set? 0 set1) 30 (intersection-set set1 set2) 31 (union-set set1 set2)