問題2.29 – SICP(計算機プログラムの構造と解釈)その44
問題2.29 (define (make-mobile left right) (list left right)) (define (make-branch length structure) (list length structure)) a. left-branch、right-branch、branch-length、branch-structure (define (left-bran…続きを読む
問題2.29 (define (make-mobile left right) (list left right)) (define (make-branch length structure) (list length structure)) a. left-branch、right-branch、branch-length、branch-structure (define (left-bran…続きを読む
問題2.28 (define (fringe items) (cond ((null? items) ()) ((not (pair? items)) (list items)) (else (append (fringe (car items)) (fringe (cdr items)))))) (define x (list (list 1 2) (list 3 4))) ; ((1 2) (…続きを読む
(define (deep-reverse items) (cond ((not (pair? items)) items) (else (reverse (map deep-reverse items))))) (define x (list (list 1 2) (list 3 4))) ; ((1 2) (3 4)) (define y (list (list (list 1 2) (lis…続きを読む
問題2.24 (list 1 (list 2 (list 3 4))) gosh> (1 (2 (3 4))) 問題2.25 (define x (list 1 3 (list 5 7) 9)) ; (1 3 (5 7) 9) (car (cdr (car (cdr (cdr x))))) gosh> 7 (define x (list (list 7))) ; ((7)) (car …続きを読む
問題2.23 (define (for-each proc items) (if (null? items) #f (and (proc (car items)) (for-each proc (cdr items))))) (for-each (lambda (x) (newline) (display x)) (list 57 321 88)) gosh> 57 321 88#f 他の人…続きを読む
問題2.22 Louis Reasoner の反復プロセスでの square-list square した値を cons により結果となる answer(リスト) に繫げていくためにリストの値の順番が反転する。 (define (square-list items) (define (iter things answer) (if (null? things) answer (iter (cdr …続きを読む
リストの写像 リストの各数値を与えられた引数倍する手続き square-list。 (define (scale-list items factor) (if (null? items) () (cons (* (car items) factor) (scale-list (cdr items) factor)))) (scale-list (list 1 2 3 4 5) 10) gosh&g…続きを読む
ドット末尾記法(dotted-tail notation) ドットの後にあるパラメータはリストになる。 (define (f x y . z) (print x y z)) (f 1 2 3 4 5) gosh> 12(3 4 5) (f 1 2) gosh> 12() (define (g . w) (print w)) (g 1 2 3 4 5) gosh> (1 2 3 4…続きを読む
問題2.19 (define us-coins (list 50 25 10 5 1)) (define uk-coins (list 100 50 20 10 5 2 1 0.5)) (define (cc amount coin-values) (cond ((= amount 0) 1) ((or (< amount 0) (no-more? coin-values)) 0) (els…続きを読む
問題2.17 (define (last-pair items) (if (null? (cdr items)) (car items) (last-pair (cdr items)))) (last-pair (list 5 2 6 10 4 8)) gosh> 8 問題2.18 (define (reverse items) (define (reverse-iter i r) (if …続きを読む