UNB/ CS/ David Bremner/ teaching/ cs4613/ lectures/ lecture06/ state3.rkt
#lang racket
(require [only-in plait test test/exn error print-only-errors])

(define (mk-o-state/priv init)
  (let ([cred init]
        [deb 0])
    (lambda (m)
      (case m
        [(inc) (lambda () (set! cred (+ 1 cred)))]
        [(dec) (lambda () (set! deb (+ 1 deb)))]
        [(get) (lambda () (- cred deb))]))))

(define-syntax snd
  (syntax-rules ()
    [(_ obj selector args ...)
     ((obj (quote selector)) args ...)]))

(test (let ([o (mk-o-state/priv 5)])
        (begin (snd o inc) (snd o inc) (snd o dec)
               (snd o get)))
      6)