#lang racket ; the constructor for a counter object, returns a closure with ; the initial value of the counter set to init. (define (create-counter init) (define counter init) ; the lambda captures the local defintion of counter (lambda (op) (cond [ (eq? op 'get) counter ] [ (eq? op 'inc) (set! counter (+ 1 counter)) counter ] [ else (error (format "Invalid operation ~a" op) ) ] )) ) ; (define c (create-counter 43)) ; (c 'get) ; (c 'inc) ; (c 'get) ; (c 'dec)