Add exercises 3.1 - 3.3
This commit is contained in:
parent
fbf1b45642
commit
6c31c289fa
|
@ -0,0 +1,6 @@
|
||||||
|
#lang sicp
|
||||||
|
|
||||||
|
(define (make-accumulator sum)
|
||||||
|
(lambda (inc)
|
||||||
|
(set! sum (+ sum inc))
|
||||||
|
sum))
|
|
@ -0,0 +1,12 @@
|
||||||
|
#lang sicp
|
||||||
|
|
||||||
|
(define (make-monitored f)
|
||||||
|
(let ((call-count 0))
|
||||||
|
(define (mf arg)
|
||||||
|
(cond ((eq? arg 'how-many-calls?) call-count)
|
||||||
|
((eq? arg 'reset-count) (set! call-count 0))
|
||||||
|
(else
|
||||||
|
(set! call-count (+ call-count 1))
|
||||||
|
(f arg))))
|
||||||
|
mf))
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
#lang sicp
|
||||||
|
|
||||||
|
(define (make-account balance account-password)
|
||||||
|
(define (withdraw amount)
|
||||||
|
(if (>= balance amount)
|
||||||
|
(begin (set! balance (- balance amount))
|
||||||
|
balance)
|
||||||
|
"Insufficient funds"))
|
||||||
|
(define (deposit amount)
|
||||||
|
(begin
|
||||||
|
(set! balance (+ balance amount))
|
||||||
|
balance))
|
||||||
|
(define (dispatch password m)
|
||||||
|
(if (eq? password account-password)
|
||||||
|
(cond ((eq? m 'withdraw) withdraw)
|
||||||
|
((eq? m 'deposit) deposit)
|
||||||
|
(else (error "Unknown request -- MAKE-ACCOUNT"
|
||||||
|
m)))
|
||||||
|
(error "Incorrect password")))
|
||||||
|
dispatch)
|
Loading…
Reference in New Issue