読者です 読者をやめる 読者になる 読者になる

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

練習問題3.3
(define (make-account balance password)
  (define (withdraw amount)
    (if (>= balance amount)
        (begin (set! balance (- balance amount ))
               balance)
        "Insufficient funds"))
  (define (deposit amount)
    (set! balance (+ balance amount ))
    balance)
  (define (dispatch pw m)
    (if (eq? pw password)
        (cond ((eq? m 'withdraw) withdraw)
              ((eq? m 'deposit) deposit)
              (else (error "Unknown request: MAKE-ACCOUNT"
                           m)))
        (error "Incorrect password")))
  dispatch)

by tube

練習問題3.4
(define (call-the-cops .x)
  (display "police"))

(define (make-account balance)
  (let ((pass-count 0))
    (define (withdraw amount)
      (if (>= balance amount)
          (begin (set! balance (- balance amount ))
            balance)
          "Insufficient funds"))
    (define (deposit amount)
      (set! balance (+ balance amount ))
      balance)
    (define (dispatch password m)
      (if (eq? password 'secret-password)
          (begin (set! pass-count 0)
            (cond ((eq? m 'withdraw) withdraw)
                  ((eq? m 'deposit) deposit)
                  (else (error "Unknown request: MAKE-ACCOUNT" m))))
          (if (= pass-count 6)
              call-the-cops
              (begin (set! pass-count (+ pass-count 1))
                (error "Incorrect password")))))
    dispatch))

by pine