Created
December 25, 2012 06:51
-
-
Save yao2030/4371936 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 3.1 | |
(define (make-accumulator init) | |
(lambda (x) | |
(set! init (+ init x)) | |
init)) | |
;; 3.2 | |
(define (make-monitored f) | |
(let ((count 0)) | |
(lambda (m) | |
(cond ((eq? m 'how-many-calls) count) | |
(else (set! count (+ 1 count)) (f m)))))) | |
;; 3.3 & 3.4 | |
(define (make-account balance password) | |
(define n 7) | |
(define (access) | |
(if (= n 0) | |
(display 'call-the-cops) | |
(set! n (- n 1)))) | |
(define (withdraw amount) | |
(if (>= balance amount) | |
(begin (set! balance (- balance amount)) | |
balance) | |
"Insufficient funds")) | |
(define (deposit amount) | |
(set! balance (+ balance amount)) | |
balance) | |
(define (dispatch p m) | |
(cond ((not (eq? p password)) (access) "Not the right password") | |
((eq? m 'withdraw) (access) withdraw) | |
((eq? m 'deposit) (access) deposit) | |
(else (access) (error "Unknown request -- MAKE_ACCOUNT" m)))) | |
dispatch) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment