39 lines
1.1 KiB
Racket
39 lines
1.1 KiB
Racket
#lang sicp
|
|
|
|
(define (make-account balance account-password)
|
|
(define bad-passwords 0)
|
|
(define (withdraw amount)
|
|
(if (>= balance amount)
|
|
(begin (set! balance (- balance amount))
|
|
balance)
|
|
"Insufficient funds"))
|
|
(define (deposit amount)
|
|
(begin
|
|
(set! balance (+ balance amount))
|
|
balance))
|
|
(define (call-the-cops)
|
|
(error "Call the cops"))
|
|
;;(let ((bad-passwords 0))
|
|
(define (dispatch password m)
|
|
(if (eq? password account-password)
|
|
(begin
|
|
(set! bad-passwords 0)
|
|
(cond ((eq? m 'withdraw) withdraw)
|
|
((eq? m 'deposit) deposit)
|
|
(else (error "Unknown request -- MAKE-ACCOUNT"
|
|
m))))
|
|
(begin
|
|
(set! bad-passwords (+ bad-passwords 1))
|
|
(if (> bad-passwords 7)
|
|
(error "Call the cops")
|
|
(error "Incorrect password")))))
|
|
dispatch)
|
|
|
|
(define (make-joint linkee-account linkee-password account-password)
|
|
(lambda (password m)
|
|
(if (eq? password account-password)
|
|
(linkee-account linkee-password m)
|
|
(error "Incorrect joint password"))))
|
|
|
|
|