登 录
这些是以前做好的,先贴上来。第一章部分习题:
;1.34 (define (f g) (g 2)) ;(f square) ;(f (lambda (z) (* z (+ z 1)))) ;(f f) ; (define (search f neg-point pos-point) ( let ((midpoint (average neg-point pos-point))) (if (close-enough? neg-point pos-point) midpoint (let ((test-value (f midpoint))) (cond ((positive? test-value) (search f neg-point midpoint)) ((negative? test-value) (search f midpoint pos-point)) (else midpoint)))))) (define tolerance 0.00001) (define (close-enough? x y) (< (abs (- x y)) tolerance)) (define (half-interval-method f a b) ( let ((a-value (f a)) (b-value (f b))) (cond ((and (negative? a-value) (positive? b-value)) (search f a b)) ((and (negative? b-value) (positive? a-value)) (search f b a)) (else (error "values are not of opposite sign" a b))))) ;(half-interval-method sin 2.0 4.0) ;(half-interval-method (lambda (x) (- (* x x x) (* 2 x) 3)) 1.0 2.0) (define (fixed-point f first-guess) (define (try guess) (let ((next (f guess))) (if (close-enough? guess next) next (try next)))) (try first-guess)) ;(fixed-point cos 1.0) ;(fixed-point (lambda (y) (+ (sin y) (cos y))) 1.0) ;1.35 ;(fixed-point (lambda (x) (+ 1 (/ 1 x))) 1.0) ;1.36 (define (fixed-point2 f first-guess) (define (try guess times) (let ((next (f guess))) (newline) (display "guess ") (display times) (display " is: ") (display next) (if (close-enough? guess next) next (try next (+ times 1))))) (try first-guess 1)) ;(fixed-point2 (lambda (x) (/ (log 1000) (log x))) 2.0) ;(fixed-point2 (lambda (x) (average x (/ (log 1000) (log x)))) 2.0) ;1.37 (define (cont-frac n d k) (define (iter i) (if (= i k) 0 (/ (n i) (+ (d i) (iter (+ i 1)))))) (iter 1)) ;(/ 1.0 (cont-frac (lambda (i) 1.0) (lambda (i) 1.0) 1000)) (define (cont-frac2 n d k) (define (iter i res) (if (= i 0) res (iter (- i 1) (/ (n i) (+ (d i) res))))) (iter k 0)) ;(/ 1.0 (cont-frac2 (lambda (i) 1.0) (lambda (i) 1.0) 1000)) ;1.38 (define (dn i) ( cond ((= i 1) 1) ((= i 2) 2) ((= 0 (remainder (- i 2) 3)) (+ 2 (/ (* 2 (- i 2)) 3))) (else 1))) ;(dn 5) ;(+ 2 (cont-frac (lambda (i) 1.0) dn 1000)) (define (average-damp f) (lambda (x) (average x (f x)))) ;((average-damp square) 10) (define (sqrt2 x) (fixed-point (average-damp (lambda (y) (/ x y))) 1.0)) ;(sqrt2 625) (define (cube-root x) (fixed-point (average-damp (lambda (y) (/ x (square y)))) 1.0)) ;(cube-root 64) (define dx 0.000001) (define (deriv g) (lambda (x) (/ (- (g (+ x dx)) (g x)) dx))) ;((deriv cube) 5) (define (newton-transform g) (lambda (x) (- x (/ (g x) ((deriv g) x))))) (define (newtons-method g guess) (fixed-point (newton-transform g) guess)) (define (sqrt3 x) ;(newtons-method (lambda (y) (/ x y)) 1.0)) ;why wrong?? (newtons-method (lambda(y) (- (square y) x)) 1.0)) ;(sqrt3 625) (define (fixed-point-of-transform g transform guess) (fixed-point (transform g) guess)) (define (sqrt4 x) (fixed-point-of-transform (lambda (y) (/ x y)) average-damp 1.0)) (define (sqrt5 x) (fixed-point-of-transform (lambda(y) (- (square y) x)) newton-transform 1.0)) ;(sqrt4 625) ;(sqrt5 625) ;1.40 (define (cubic a b c) (lambda(x) (+ (cube x) (* a (square x)) (* b x) c))) ;(newtons-method (cubic a b c) 1) ;(newtons-method (cubic 2 2 3) 1) ;1.41 (define (double f) (lambda(x) (f (f x)))) ;(((double (double double)) inc) 5) ;1.42 (define (compose f g) (lambda(x) (f (g x)))) ;((compose square inc) 6) ;1.43 (define (repeated f n) (define (iter i) (if (= i 1) f (compose f (iter (- i 1))))) (iter n)) ;((repeated square 2) 5) ;1.44 (define (smooth f) (lambda(x) (/ (+ (f (+ x dx)) (f x) (f (- x dx))) 3))) (define (smooth-n f n) ((repeated smooth n) f)) ;((smooth-n square 2) 5) ;1.45 (define (nth-power-root x n) (fixed-point ((repeated average-damp (/ (log n) (log 2))) (lambda (y) (/ x (expr y (- n 1))))) 1.0)) ;(nth-power-root 64 3) ;(nth-power-root 16 4) ;(nth-power-root 64 32) ;(nth-power-root 64 64) ;(/ (log 1024) (log 2)) ;1.46 (define (iterative-improve good? improve-method) (define (iter-imp guess x) (if (good? guess x) guess (iter-imp (improve-method guess x) x))) (lambda (guess x) (iter-imp guess x))) (define (sqrt6 x) ((iterative-improve good-enough? improve) 1.0 x)) (sqrt6 9) ;(sqrt2 9)
第二章部分习题:
;2.1 (define (make-rat a b) (let ((g (gcd (abs a) (abs b)))) (cond ((and (< a 0) (< b 0)) (cons (/ (abs a) g) (/ (abs b) g))) ((< b 0) (cons (/ (- a) g) (/ (abs b) g))) (else (cons (/ a g) (/ b g)))))) (define (numer x) (car x)) (define (denom x) (cdr x)) (define (print-rat x) (newline) (display (numer x)) (display "/") (display (denom x))) ;(print-rat (mul-rat (make-rat 2 4) (make-rat 1 -5))) ;(print-rat (sub-rat (make-rat 1 4) (make-rat 1 2))) ;(print-rat (add-rat (make-rat 2 4) (make-rat 1 4))) ;(print-rat (div-rat (make-rat 2 4) (make-rat 1 4))) ;(print-rat (make-rat 2 4)) ;2.2 (define (make-segment s t) (cons s t)) (define (start-segment seg) (car seg)) (define (end-segment seg) (cdr seg)) (define (make-point x y) (cons x y)) (define (x-point p) (car p)) (define (y-point p) (cdr p)) (define (print-point p) (newline) (display "(") (display (x-point p)) (display ",") (display (y-point p)) (display ")")) (define (midpoint-segment seg) (let ((s (start-segment seg)) (t (end-segment seg))) (make-point (/ (+ (x-point s) (x-point t)) 2) (/ (+ (y-point s) (y-point t)) 2)))) (define a-seg (make-segment (make-point 1 2) (make-point 5 8))) ;(print-point (start-segment a-seg)) ;(print-point (end-segment a-seg)) ;(print-point (midpoint-segment a-seg)) ;2.3 (define (make-rect lbp rtp) (cons lbp rtp)) (define (get-long rect) (let ((lbp (car rect)) (rtp (cdr rect))) (- (x-point rtp) (x-point lbp)))) (define (get-width rect) (let ((lbp (car rect)) (rtp (cdr rect))) (- (y-point rtp) (y-point lbp)))) ;计算长方形周长 (define (rect-perim long width) (* (+ long width) 2)) (define (rect-area long width) (* long width)) (define a-rect (make-rect (make-point 0 0) (make-point 2 8))) ;(rect-perim (get-long a-rect) (get-width a-rect)) ;(rect-area (get-long a-rect) (get-width a-rect)) ;2.4 (define (cons1 x y) (lambda (m) (m x y))) (define (car1 z) (z (lambda (p q) p))) (define (cdr1 z) (z (lambda (p q) q))) ;(car1 (cons1 3 5)) ;(cdr1 (cons1 3 5)) ;2.5 (define (cons2 x y) (* (exp (* x (log 2))) (exp (* y (log 3))))) ; 没找到求幂的库函数来计算2^x * 3^y (define (car2 z) (define (iter n z) (if (= (remainder z 2) 0) (iter (+ n 1) (/ z 2)) n)) (iter 0 z)) (define (cdr2 z) (define (iter n z) (if (= (remainder z 3) 0) (iter (+ n 1) (/ z 3)) n)) (iter 0 z)) ;(car2 (cons2 3 7)) ;(cdr2 (cons2 3 7)) ;(car2 17496) ;(cdr2 17496) ;2.6 ;2.7 (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 (div-interval x y) (mul-interval x (make-interval (/ 1.0 (upper-bound y)) (/ 1.0 (lower-bound y))))) (define (make-interval a b) (cons a b)) (define (lower-bound x) (car x)) (define (upper-bound x) (cdr x)) (define (print-interval x) (newline) (display "[") (display (lower-bound x)) (display ", ") (display (upper-bound x)) (display "]")) ;(print-interval (add-interval (make-interval 1.2 1.4) (make-interval 3.5 4.0))) ;(print-interval (mul-interval (make-interval 1 2) (make-interval 3 8))) ;(print-interval (div-interval (make-interval 2 8) (make-interval 1 2))) ;2.8 (define (sub-interval x y) (make-interval (- (lower-bound x) (upper-bound y)) (- (upper-bound x) (lower-bound y)))) ;(print-interval (sub-interval (make-interval 3 6) (make-interval 1 2))) ; ;2.9 ;... ;(car (cdr (list 1 2 3 4 5))) ;(cadddr (list 1 2 3 4)) (define squares (list 1 4 9 16 25)) (define odds (list 1 3 5 7)) (define (append1 list1 list2) (if(null? list1) list2 (cons (car list1) (append1 (cdr list1) list2)))) ;(append1 squares odds) ;(append1 odds squares) ;2.17 ;(if (cdr (list 1 2)) 'nil ; 'not-nil) (define (last-pair list1) (cond ((or (null? list1) (null? (cdr list1))) '()) ((null? (cddr list1)) (cdr list1)) (else (last-pair (cdr list1))))) ;(last-pair (list 23 72 149 34)) ;2.18 (define (reverse1 list1) (if(or (null? list1) (null? (cdr list1))) list1 (append (reverse1 (cdr list1)) (list (car list1))))) ;(reverse1 (list 1 4 9 16 25)) ;(list 3) ;(cons 3 '()) ;2.19 (from 1.2.2) (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))) ;(count-change 100) ; (define us-coins (list 50 25 10 5 1)) (define uk-coins (list 100 50 20 10 5 2 1 0.5)) (define (cc1 amount coin-values) (cond ((= amount 0) 1) ((or (< amount 0) (no-more? coin-values)) 0) (else (+ (cc1 amount (except-first-denomination coin-values)) (cc1 (- amount (first-denomination1 coin-values)) coin-values))))) (define (no-more? coin-values) (null? coin-values)) (define (except-first-denomination coin-values) (cdr coin-values)) (define (first-denomination1 coin-values) (car coin-values)) ;(cc1 100 us-coins) ;(cc1 100 uk-coins) ; ;2.20 ; (define (filter pred? seq) (cond ((null? seq) '()) ((pred? (car seq)) (cons (car seq) (filter pred? (cdr seq)))) (else (filter pred? (cdr seq))))) ;(map (lambda(x) (cond ((= (remainder x 2) 0) x))) (list 1 2 3 4 5 6)) ;(filter (lambda(x) (if (= (remainder x 2) 0) true false)) (list 1 2 3 4 5 6)) (define (same-parity first . others) (if (odd? first) (cons first (filter odd? others)) (cons first (filter even? others)))) ;(same-parity 1 2 3 4 5 6 7) ;(same-parity 2 3 4 5 6 7) ;2.21 (define (square x) (* x x)) (define (square-list items) (if (null? items) '() (cons (square (car items)) (square-list (cdr items))))) (define (square-list1 items) (map square items)) ;(square-list (list 1 2 3 4)) ;(square-list1 (list 1 2 3 4)) ; ;2.22 ;cons是非对称的, ;cons 一个list和一个elem,不能形成一个线性的list ;但cons 一个elem和一个list,可以形成一个线性的list ;(cons 4 5) ;(cons 3 (list 4 5)) ;(cons (list 4 5) 3) ;(list (list 4 5) 3) ;(list 3 (list 4 5) ) ;(append (list 4 5) (list 3)) ;(append (list 3) (list 4 5) ) ;2.23 (define (for-each1 func items) (if (null? items) true (and (func (car items)) (for-each1 func (cdr items))))) ;(for-each1 (lambda (x) (newline) (display x)) (list 57 232 88)) ;(cons (list 1 2) (list 3 4)) ;(cons (list 1 2) (list (list 3 4))) ;2.24 ;(pair? (cons (list 1 2) (list 3 4))) ; ;(list 1 (list 2 (list 3 4))) ;(1 (2 (3 4))) ; ;2.25 ; ;(car (cdr (car (cdr (cdr (list 1 3 (list 5 7) 9)))))) ;(caar (list (list 7))) ;(car (cadadr (cadadr (cadadr (list 1 (list 2 (list 3 (list 4 (list 5 (list 6 (list 7))))))))))) ; ;2.27 (define x (list (list 1 2) (list 3 4))) (define (deep-reverse items) (if (null? items)'() (append (deep-reverse (cdr items)) (if (pair? (car items) ) (list (deep-reverse (car items))) (list (car items)))))) ;(reverse1 x) ;(deep-reverse x) ;2.28 (define (fringe items) (if (null? items) '() (append (if (pair? (car items)) (fringe (car items)) (list (car items))) (fringe (cdr items))))) ;(fringe x) ;(fringe (list x x)) ;2.29 ;(cdr (cons 2 3)) ;(cdr (list 2 3)) ;a) ;(define (make-mobile left right) ; (list left right)) ;(define (make-branch length structure) ; (list length structure)) ;(define (left-branch mobile) ; (car mobile)) ;(define (right-branch mobile) ; (cadr mobile)) ;(define (branch-length br) ; (car br)) ;(define (branch-structure br) ; (cadr br)) ;d) (define (make-mobile left right) (cons left right)) (define (make-branch length structure) (cons length structure)) (define (left-branch mobile) (car mobile)) (define (right-branch mobile) (cdr mobile)) (define (branch-length br) (car br)) (define (branch-structure br) (cdr br)) ;b) (define (br-weight br) (let ((bs (branch-structure br))) (if (pair? bs) (total-weight bs) bs))) (define (total-weight mobile) (+ (br-weight (left-branch mobile)) (br-weight (right-branch mobile)))) (define m1 (make-mobile (make-branch 2 3) (make-branch 1 2))) (define m2 (make-mobile (make-branch 5 8) (make-branch 3 m1))) ;(total-weight m2) ;c) (define (mobile-balanced? mobile) (define (branch-balanced? br) (let ((bs (branch-structure br))) (if (pair? bs) (mobile-balanced? bs) true))) (let ((lbr (left-branch mobile)) (rbr (right-branch mobile))) (and (branch-balanced? lbr) (branch-balanced? rbr) (= (* (branch-length lbr) (br-weight lbr)) (* (branch-length rbr) (br-weight rbr)))))) (define m3 (make-mobile (make-branch 4 9) (make-branch 6 6))) (define m4 (make-mobile (make-branch 6 5) (make-branch 2 m3))) ;(mobile-balanced? m1) ;(mobile-balanced? m2) ;(mobile-balanced? m3) ;(mobile-balanced? m4) ;2.30 (define (square-tree items) (if (null? items) '() (map (lambda (x) (if (pair? x) (square-tree x) (square x))) items))) (define (square-tree1 items) (if (null? items) '() (append (if (pair? (car items)) (list (square-tree1 (car items))) (list (square (car items)))) (square-tree1 (cdr items))))) (define (square-tree2 items) (if (null? items) '() (cons (if (pair? (car items)) (square-tree2 (car items)) (square (car items))) (square-tree2 (cdr items))))) ;(square-tree2 (list 1 (list 2 (list 3 4) 5) (list 6 7))) ;2.31 (define (tree-map proc tree) (if (null? tree) '() (map (lambda (x) (if (pair? x) (tree-map proc x) (proc x))) tree))) (define (square-tree3 tree) (tree-map square tree)) ;(square-tree3 (list 1 (list 2 (list 3 4) 5) (list 6 7))) ;2.32 (define (subset s) (if (null? s) (list '()) (let ((rest (subset (cdr s)))) (append rest (map (lambda (x) (cons (car s) x)) rest))))) ;(subset (list 1 2 3 4)) (define (sum-odd-squares tree) (cond ((null? tree) 0) ((not (pair? tree)) (if (odd? tree) (square tree) 0)) (else (+ (sum-odd-squares (car tree)) (sum-odd-squares (cdr tree)))))) ;(sum-odd-squares (list 1 (list 2 (list 3 4) 5) (list 6 7))) (define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2)))))) (define (even-fibs n) (define (next k) (if (> k n) '() (let ((f (fib k))) (if (even? f) (cons f (next (+ k 1))) (next (+ k 1)))))) (next 0)) ;(even-fibs 10) (define (enum-tree tree) (cond ((null? tree) '()) ((pair? tree) (append (enum-tree (car tree)) (enum-tree (cdr tree)))) (else (list tree)))) ;(enum-tree (list 1 (list 2 (list 3 4) 5) (list 6 7))) (define (accumulate1 proc init_val items) ;这个实现竟在练习2.33中运行不正确!为什么?? (if (null? items) init_val (accumulate1 proc (proc init_val (car items)) (cdr items)))) ;因为它(proc了init_val,而init_val有可能为空!用户传进来的proc,可能会对init_val进行操作,所以就挂了 (define (accumulate proc init_val items) (if (null? items) init_val (proc (car items) (accumulate proc init_val (cdr items))))) ;(accumulate + 0 (map square (filter odd? (enum-tree (list 1 (list 2 (list 3 4) 5) (list 6 7)))))) ; ;(accumulate * 1 (map square (filter odd? (enum-tree (list 1 (list 2 (list 3 4) 5) (list 6 7)))))) (define (enum-interval n) (if (= n 0) (list 0) (append (enum-interval (- n 1)) (list n)))) ;(filter even? (map fib (enum-interval 10))) ;2.33 (define (map1 p sequence) (accumulate (lambda (x y) (cons (p x) y)) '() sequence)) (define (append2 seq1 seq2) (accumulate cons seq2 seq1 )) (define (length1 sequence) (accumulate (lambda (x y) (+ 1 y)) 0 sequence)) ;(map1 square (list 1 2 3 4 5)) ;(append2 (list 1 2 3) (list 4 5)) ;(length1 (list 1 2 3 4 5)) ;2.34 (define (horner-eval x coefficient-sequence) (accumulate (lambda (this-coeff higher-terms) (+ this-coeff (* x higher-terms))) 0 coefficient-sequence)) ;(horner-eval 2 (list 1 3 0 5 0 1)) ; ;2.35 (define (count-leaves t) (accumulate + 0 (map (lambda (x) (if (pair? x) (count-leaves x) 1)) t))) (define (count-leaves1 t) (accumulate (lambda (x y) (if (pair? x) (+ (count-leaves1 x) y) (+ 1 y))) 0 t)) (define xe (cons (list 1 2) (list 3 4))) ;(count-leaves xe) ;(count-leaves (list xe xe)) ;(count-leaves1 xe) ;(count-leaves1 (list xe xe)) ; ; ;2.36 (define (accumulate-n op init seqs) (if (null? (car seqs)) '() (cons (accumulate op init (map car seqs)) (accumulate-n op init (map cdr seqs))))) ;(define s (list (list 1 2 3) (list 4 5 6) (list 7 8 9) (list 10 11 12))) ;(accumulate-n + 0 s) ; ; ;2.37 ; (define (dot-product v w) (accumulate + 0 (map * v w))) (define (matrix-*-vector m v) (map (lambda (x) (dot-product x v)) m)) (define (transpose mat) (accumulate-n cons '() mat)) (define (matrix-*-matrix m n) (let ((cols (transpose n))) (map (lambda (x) (matrix-*-vector cols x)) m))) (define m (list (list 1 2 3 4) (list 4 5 6 6) (list 6 7 8 9))) (define v (list 2 2 2 2)) (define w (list 3 3 3 3)) (define n (list (list 1 1) (list 1 1) (list 1 1) (list 1 1))) ;(dot-product v w) ;(matrix-*-vector m v) ;(transpose m) ;(transpose n) ;(matrix-*-matrix m n) ; ; ;2.38 ;(accumulate list '() (list 1 2 3)) ; (define (fold-left op initial sequence) (define (iter result rest) (if (null? rest) result (iter (op result (car rest)) (cdr rest)))) (iter initial sequence)) (define (fold-right op initial sequence) (accumulate op initial sequence)) ;(fold-right / 1 (list 1 2 3)) ;(fold-left / 1 (list 1 2 3)) ;(fold-right list '() (list 1 2 3)) ;(fold-left list '() (list 1 2 3)) ;2.39 (define (reverse2 sequence) (fold-right (lambda (x y) (append y (list x))) '() sequence)) (define (reverse3 sequence) (fold-left (lambda (x y) (cons y x)) '() sequence)) (reverse2 (list 1 2 3 4)) (reverse3 (list 1 2 3 4))
抱歉!评论已关闭.