SICPゼミ第19回
練習問題3.24
(define (make-table same-key?) (let ((table (list '*table*))) (define (lookup key) (let ((record (assoc key (cdr table)))) (if record (cdr record) #f))) (define (assoc key records) (cond ((null? records) #f) ((same-key? key (caar records)) (car records)) (else (assoc key (cdr records))))) (define (insert! key value) (let ((record (assoc key (cdr table)))) (if record (set-cdr! record value) (set-cdr! table (cons (cons key value) (cdr table ))))) 'ok) (define (dispatch m) (cond ((eq? m 'lookup) lookup) ((eq? m 'insert!) insert!))) dispatch))
by pine
SICPゼミ第18回
練習問題3.21
(define (print-queue queue) (display (car queue)))
Benはqueueの末尾のポインタを見てはっちゃけてるだけ。
by pine
練習問題3.22
(define (make-queue) (let ( (front-ptr '()) (rear-ptr '()) ) (define (empty-queue?) (null? front-ptr)) (define (insert-queue item) (let ((paired (cons item '()))) (if (empty-queue?) (begin (set! front-ptr paired) (set! rear-ptr paired)) (begin (set-cdr! rear-ptr paired) (set! rear-ptr paired)) ) ) ) (define (delete-queue) (if (empty-queue?) (error "なにもない") (set! front-ptr (cdr front-ptr)) ) ) (define (front-queue) (if (empty-queue?) (error "なにもない") (car front-ptr) ) ) (define (dispatch m) (cond ((eq? m 'insert) insert-queue) ((eq? m 'delete) (delete-queue)) ((eq? m 'front) (front-queue)) ((eq? m 'empty?) (empty-queue?)) (else (error "そんな手続きはない")) ) ) dispatch ) )
by dolicas
SICPゼミ第17回
練習問題3.12
1つめの (cdr x) は (b)
2つめの (cdr x) は (b c d)
1つめでは x が (a b) なので (b) が返る。
2つめでは x が append! にある set-cdr! のせいで (a b c d) になっているので (b c d) が返る。
by tube
練習問題3.13
a -> b -> c -> a ->.....
となっているので last-pair の (if (null? (cdr x))) が永遠に #t 評価に入らず無限に last-pair が呼び出される。
by tube
SICPゼミ第16回
練習問題3.10
SICPゼミ第15回
練習問題3.5, 3.6 は乱数生成のやつがなんかうまくいかないので飛ばし。
練習問題3.7
(define (make-joint acc password newpassword) (define (dispatch2 pw m) (if (eq? pw newpassword) (acc password m) (error "Incorrect password"))) (if (= ((acc password 'withdraw) 0) ((acc password 'deposit) 0)) dispatch2 (error "Incorrect password")))
by 現実
SICPゼミ第14回
3章のテーブル演算の実装を読んでから2章の残りに帰ってくることにしました。
練習問題3.1
(define (make-accumulator initial) (lambda (amount) (begin (set! initial (+ initial amount)) initial)))
by pine
練習問題3.2
(define (make-monitored func) (define count 0) (define (dispatch m) (if (eq? m 'how-many-calls?) count (begin (set! count (+ count 1)) (func m)) ) ) dispatch )
by dolicas
SICPゼミ第13回
今日も人生がつらい。
Gauche ユーザリファレンス: 7.4 ジェネリックファンクションとメソッド
練習問題2.75
(define (make-from-mag-ang r theta) (define (dispatch op) (cond ((eq? op 'real-part) (* r (cos theta))) ((eq? op 'imag-part) (* r (sin theta))) ((eq? op 'magnitude) r) ((eq? op 'angle) theta) (else (error "Unknown op: MAKE-FROM-REAL-IMAG" op)))) dispatch)
by tube
練習問題2.76
演算が追加されやすい→data-directed
型が追加されやすい→message-passing