]> git.rkrishnan.org Git - sicp.git/blob - src/sicp/ex3_4.rkt
Solutions to 4.27, 4.28 and 4.29.
[sicp.git] / src / sicp / ex3_4.rkt
1 #lang racket
2
3 (define (make-account balance password)
4   (define (withdraw amount)
5     (if (>= balance amount)
6         (begin (set! balance (- balance amount))
7                balance)
8         "Insufficient funds"))
9   (define (deposit amount)
10     (set! balance (+ balance amount))
11     balance)
12   (let ((bad-password-count 0))
13     (define (dispatch given-password m)
14       (if (eq? given-password password)
15           (cond ((eq? m 'withdraw) (lambda (amt) (withdraw amt)))
16                 ((eq? m 'deposit) (lambda (amt) (deposit amt)))
17                 (else (error "Unknown request -- MAKE-ACCOUNT"
18                              m)))
19           (lambda (temp) 
20             (begin
21               (set! bad-password-count (+ bad-password-count 1))
22               (if (>= bad-password-count 7)
23                   "Something is wrong. Calling 911..."
24                   "Incorrect password")))))
25     dispatch))
26
27 #|
28
29 > (define acc (make-account 100 'secret-password))
30 > ((acc 'secret-password 'withdraw) 40)
31 60
32 > ((acc 'some-other-password 'deposit) 50)
33 "Incorrect password"
34 > ((acc 'some-other-password 'deposit) 50)
35 "Incorrect password"
36 > ((acc 'some-other-password 'deposit) 50)
37 "Incorrect password"
38 > ((acc 'some-other-password 'deposit) 50)
39 "Incorrect password"
40 > ((acc 'some-other-password 'deposit) 50)
41 "Incorrect password"
42 > ((acc 'some-other-password 'deposit) 50)
43 "Incorrect password"
44 > ((acc 'some-other-password 'deposit) 50)
45 "Something is wrong. Calling 911..."
46
47
48 |#