diff --git a/3_1.rkt b/3_1.rkt new file mode 100644 index 0000000..cc4f038 --- /dev/null +++ b/3_1.rkt @@ -0,0 +1,6 @@ +#lang sicp + +(define (make-accumulator sum) + (lambda (inc) + (set! sum (+ sum inc)) + sum)) diff --git a/3_2.rkt b/3_2.rkt new file mode 100644 index 0000000..9693698 --- /dev/null +++ b/3_2.rkt @@ -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)) + diff --git a/3_3.rkt b/3_3.rkt new file mode 100644 index 0000000..6e4e1ab --- /dev/null +++ b/3_3.rkt @@ -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)