#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 args ; if no param list, return the counter (if (null? args) counter (let ( [ op (first args) ] ) (cond [ (eq? op 'get) counter ] [ (eq? op 'inc) (set! counter (+ 1 counter)) counter ] [ (eq? op 'inc-by) (set! counter (+ (second args) counter)) counter ] [ else (error (format "Invalid operation ~a" op ) ) ] )) ) )) ; (define o1 (create-counter 0)) ; ; (o1 'inc) ; (o1) ; (o1 'inc-by 2) ; (o1)