(wat-aro)

生きてます

2015-10-26から1日間の記事一覧

SICP 問題 2.63

(define (tree->list-1 tree) (if (null? tree) '() (append (tree->list-1 (left-branch tree)) (cons (entry tree) (tree->list-1 (right-branch tree)))))) (define (tree->list-2 tree) (define (copy-to-list tree result-list) (if (null? tree) resul…

SICP 問題 2.62

(define (union-set s t) (cond ((null? s) t) ((null? t) s) ((= (car s) (car t)) (cons (car s) (union-set (cdr s) (cdr t)))) ((< (car s) (car t)) (cons (car s) (union-set (cdr s) t))) ((< (car t) (car s)) (cons (car t) (union-set s (cdr t)))…

SICP 問題 2.61

(define (adjoin-set x s) (cond ((null? s) (list x)) ((= x (car s)) s) ((< x (car s)) (cons x s)) (else (cons (car s) (adjoin-set x (cdr s)))))) ;; 同じ数字,またはxより大きい数字が出てきた時点で計算が終わるので順序付けられない表現に比べ半…

SICP 問題 2.60

;; element-of-set? intersection-setはそのまま (define (adjoin-set x s) (cons x s)) (define (union-set s t) (append s t)) ;; element-of-set?やintersection-setについてはsetの中身が増えることで比較回数が増えて効率は下がる. ;; adjoin-set unio…

SICP 問題 2.95

(define (element-of-set? x set) (cond ((null? set) false) ((equal? x (car set)) true) (else (element-of-set? x (cdr set))))) (define (unionset s t) (cond ((null? s) t) ((element-of-set? (car s) t) (unionset (cdr s) t)) (else (cons (car s) …

SICP 問題 2.58

;; a (define (make-sum a1 a2) (cond ((=number? a1 0) a2) ((=number? a2 0) a1) ((and (number? a1) (number? a2)) (+ a1 a2)) (else (list a1 '+ a2)))) (define (make-product m1 m2) (cond ((or (=number? m1 0) (=number? m2 0)) 0) ((=number? m1 1)…

SICP 問題 2.57

(define (augend s) (if (null? (cdddr s)) (caddr s) (cons '+ (cddr s)))) (define (multiplicand p) (if (null? (cdddr p)) (caddr p) (cons '* (cddr p)))) これ作るので精一杯でした. make-sumやmake-productを可変長引数に対応できるように変更するの…