Skip to content

Instantly share code, notes, and snippets.

@svetlyak40wt
Created February 8, 2017 18:42
Show Gist options
  • Save svetlyak40wt/069c8e8ed341d1a6ca21250662d89c9d to your computer and use it in GitHub Desktop.
Save svetlyak40wt/069c8e8ed341d1a6ca21250662d89c9d to your computer and use it in GitHub Desktop.
Common Lisp. Dynamic variable binding using defun's lambda expression.
(defvar *foo* nil)
(defun minor ()
(format t "~&*foo* in minor: ~a~%" *foo*))
(defun bar (&key ((:baz *foo*) 42))
(format t "~&*foo* in bar: ~a~%" *foo*)
(minor))
(progn
(format t "~&*foo* in global scope: ~a~%" *foo*)
(bar :baz 'checking))
;; This code outputs:
;; *foo* in global scope: NIL
;; *foo* in bar: CHECKING
;; *foo* in minor: CHECKING
;; Here *foo* behaves as a special variable,
;; which makes this bar definition almost equivalent to
(defun bar (&key (baz *foo*))
(let ((*foo* baz))
(format t "~&*foo* in bar: ~a~%" *foo*)
(minor)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment