-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathwithdraw.scm
76 lines (63 loc) · 1.52 KB
/
withdraw.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#lang racket
;; P150 - [3.1.1 局部状态变量]
(define balance 100)
(define (withdraw amount)
(if (>= balance amount)
(begin
(set! balance (- balance amount))
balance)
"Insufficient funds"))
(define new-withdraw
(let ((balance 100))
(lambda (amount)
(if (>= balance amount)
(begin
(set! balance (- balance amount))
balance)
"Insufficient funds"))))
(define (make-withdraw balance)
(lambda (amount)
(if (>= balance amount)
(begin
(set! balance (- balance amount))
balance)
"Insufficient funds")))
(define (make-account balance)
(define (withdraw amount)
(if (>= balance amount)
(begin
(set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (dispatch m)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT" m))))
dispatch)
;;;;;;;;;;;;;;;;;;;
(displayln "withdraw")
(withdraw 25)
(withdraw 25)
(withdraw 60)
(withdraw 15)
(displayln "new-withdraw")
(new-withdraw 25)
(new-withdraw 25)
(new-withdraw 60)
(new-withdraw 15)
(displayln "make-withdraw")
(define W1 (make-withdraw 100))
(define W2 (make-withdraw 100))
(W1 50)
(W2 70)
(W2 40)
(W1 40)
(displayln "make-account")
(define acc (make-account 100))
((acc 'withdraw) 50)
((acc 'withdraw) 60)
((acc 'deposit) 40)
((acc 'deposit) 60)