;; Dynamic versus lexical scope ;; see librep.info: Variables, Scope and Extent (require 'stdlib) ;; defvar defines a special (i.e. dynamically scoped) variable (defvar a 'aap) ;; define defines a lexically scoped variable (define b 'noot) (setq c 'mies) (defun f () (let ((a 'wim) (b 'zus) (c 'jet)) (printf "%s %s %s\n" a b c) (g))) (defun g () (printf "%s %s %s\n" a b c)) ;; prints: wim zus jet, wim noot mies ;; when g is invoked from f, a stays dynamically bound to its value in the ;; closure of f, and b is lexically bound to the global b. (f) ;; prints: aap noot mies (g) ;; example of a peristent lexical binding. The counter variable is only ;; accessible from forms written inside the let form declaring it. (let ((counter 0)) (defun count () (setq counter (1+ counter)) counter)) (puts (count)) ; => 1 (puts (count)) ; => 2 (puts (count)) ; => 3