SE701:closure.lisp

From Marks Wiki
Jump to navigation Jump to search
;;; A simple closure in Lisp
(defun make-counter ()
  (let ((count 0))
    (list #'(lambda () (setq count (1+ count)))
	  #'(lambda () (setq count (1- count)))
	  #'(lambda () count))))

(destructuring-bind (inc dec read) (make-counter)
  (funcall inc)
  (funcall inc)
  (funcall inc)
  (funcall dec)
  (print (funcall read)))