
;;; Example of records with state

(define m20
  (make-module
   'add (lambda (x) (set! total (+ total x)) total)
   'total 0))
;Value: m20

(define mi20 (module-instantiate m20))
;Value: mi20

(define add20 (record-select 'add mi20))
;Value: add20

(add20 10)
;Value: 10

(add20 4)
;Value: 14

(add20 13)
;Value: 27

mi20
;Value: ((total . 0) (add . #[compound-procedure 7]))


(define m21
  (make-module
   'add1 (lambda () (set! total (+ total 1)) total)
   'add2 (lambda () (set! total (+ total 2)) total)
   'total 0))
;Value: m21

(define mi21 (module-instantiate m21))
;Value: mi21

(define add1 (record-select 'add1 mi21))
;Value: add1

(define add2 (record-select 'add2 mi21))
;Value: add2

(add1)
;Value: 1

(add2)
;Value: 3

(add1)
;Value: 4

(add2)
;Value: 6



;;; Another instance of m21

(define mi21-2 (module-instantiate m21))
;Value: mi21-2

(define add1-2 (record-select 'add1 mi21-2))
;Value: add1-2

(define add2-2 (record-select 'add2 mi21-2))
;Value: add2-2

(add2-2)
;Value: 2

(add2-2)
;Value: 4

(add2-2)
;Value: 6

(add2-2)
;Value: 8

(add1-2)
;Value: 9

(add1)
;Value: 7

(add1)
;Value: 8

(add2-2)
;Value: 11

(add2-2)
;Value: 13

mi21
;Value: ((total . 0) (add2 . #[compound-procedure 8]) (add1 . #[compound-procedure 9]))

mi21-2
;Value: ((total . 0) (add2 . #[compound-procedure 10]) (add1 . #[compound-procedure 11]))

