(defun counter (&optional (initVal 0))
; initializes a counter to 0,
; and returns a function that acts as a dispatcher
; thereafter, the user can call the dispatcher with
; either of two options: incr, get
; - get returns the current counter value
; - incr adds one to the current counter value then returns it
(let ; the counter's private/hidden data
((count initVal))
; the two available methods
(labels (
;....................................................................
;... implement the two methods, incr and get, that belong here ......
;....................................................................
)
; the dispatch function, built and returned as a lambda, that
; takes one of the two commands, incr or get, as a parameter
(lambda (cmd)
;..................................................
;... implement the dispatcher that belongs here ...
;..................................................
))))
; example calls/use
(setf ctr (counter 3))
(format t "counter after initialized with 3: ~A~%" (funcall ctr 'get))
(format t "increment counter: ~A~%" (funcall ctr 'incr))
(format t "increment again: ~A~%" (funcall ctr 'incr))
(format t "current counter: ~A~%" (funcall ctr 'get))
|