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の効率が悪い。これ役に立つシチュエーションってあるの?

続きを読む

SICPゼミ第10回

練習問題2.53
gosh> (list 'a 'b 'c)
(a b c)
gosh> (list (list 'george ))
((george))
gosh> (cdr '((x1 x2) (y1 y2)))
((y1 y2))
gosh> (cadr '((x1 x2) (y1 y2)))
(y1 y2)
gosh> (pair? (car '(a short list )))
#f
gosh> (memq 'red '((red shoes) (blue socks )))
#f
gosh> (memq 'red '(red shoes blue socks ))
(red shoes blue socks)

by pine

続きを読む