Skip to content

Commit 0786570

Browse files
committed
Add exercise 3-7
1 parent 2958ae4 commit 0786570

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

chapter3/exercise3-3.rkt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@
3535
))
3636

3737
(run-tests module-test))
38+
39+
(provide make-account)

chapter3/exercise3-7.rkt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#lang racket
2+
(require "exercise3-3.rkt")
3+
(define (make-joint account old-password new-password)
4+
(lambda (pwd m)
5+
(if (eq? pwd new-password)
6+
(account old-password m)
7+
(error "Incorrect joint account password"))))
8+
9+
(module+ test
10+
(require rackunit)
11+
(require rackunit/text-ui)
12+
(define peter-acc (make-account 100 'open-sesame))
13+
(define paul-acc (make-joint peter-acc 'open-sesame 'rosebud))
14+
15+
(define module-test
16+
(test-suite
17+
"Tests for make-joint"
18+
(check-equal? ((peter-acc 'open-sesame 'withdraw) 40) 60 "Test original account with old password")
19+
(check-equal? ((paul-acc 'rosebud 'withdraw) 40) 20 "Test joint account with new password")
20+
(check-exn (regexp "Incorrect joint account password")
21+
(lambda () ((paul-acc 'wrong-password 'deposit) 50)))
22+
(check-equal? ((peter-acc 'open-sesame 'deposit) 30) 50 "Test original account with old password still works")
23+
))
24+
25+
(run-tests module-test))

0 commit comments

Comments
 (0)