Add exercises 3.1 - 3.3

This commit is contained in:
Oliver Payne 2022-04-18 23:00:16 +01:00
parent fbf1b45642
commit 6c31c289fa
3 changed files with 38 additions and 0 deletions

6
3_1.rkt Normal file
View File

@ -0,0 +1,6 @@
#lang sicp
(define (make-accumulator sum)
(lambda (inc)
(set! sum (+ sum inc))
sum))

12
3_2.rkt Normal file
View File

@ -0,0 +1,12 @@
#lang sicp
(define (make-monitored f)
(let ((call-count 0))
(define (mf arg)
(cond ((eq? arg 'how-many-calls?) call-count)
((eq? arg 'reset-count) (set! call-count 0))
(else
(set! call-count (+ call-count 1))
(f arg))))
mf))

20
3_3.rkt Normal file
View File

@ -0,0 +1,20 @@
#lang sicp
(define (make-account balance account-password)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(begin
(set! balance (+ balance amount))
balance))
(define (dispatch password m)
(if (eq? password account-password)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT"
m)))
(error "Incorrect password")))
dispatch)