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ゼミ第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

続きを読む

SICPゼミ第11回

練習問題2.59
(define (union-set set1 set2)
	(cond
		((null? set1) set2)
		(else (adjoin-set (car set1) (union-set (cdr set1) set2)))
		)
	)

by dolicas

練習問題2.60

union-set、adjoin-setは実装が楽で計算も軽い。intersectionの効率が悪い。これ役に立つシチュエーションってあるの?

続きを読む