SE701:closure.lisp

From Marks Wiki
Revision as of 19:40, 15 February 2008 by Mark (talk | contribs) (New page: ;;; A simple closure in Lisp (defun make-counter () (let ((count 0)) (list #'(lambda () (setq count (1+ count))) #'(lambda () (setq count (1- count))) #'(lambda () count)...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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)))