SHARE
TWEET

Working Closure

timotheosh Dec 1st, 2016 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defvar *foo* nil)
  2.  
  3. (defun foo-test()
  4.   (unless (functionp *foo*)
  5.     (error "Please bind *foo* to a function"))
  6.   (funcall *foo*))
  7.  
  8. (defun define-foo ()
  9.   (let ((foo "foo")
  10.         (bar "bar"))
  11.     (lambda ()
  12.       (values foo bar))))
  13.  
  14. (defmacro foo-mac (&body body)
  15.   `(let ((*foo* (define-foo)))
  16.     ,@body))
  17.  
  18. (defun show-foo ()
  19.   (multiple-value-bind (key1 key2)
  20.       (foo-test)
  21.     (format t "~A ~A~%" key1 key2)))
  22.  
  23. (defun test ()
  24.   (foo-mac
  25.     (show-foo)))
  26.  
  27. (test)
RAW Paste Data
Top