[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Lexical and Dynamic Scope
From: |
Aurélien Aptel |
Subject: |
Re: Lexical and Dynamic Scope |
Date: |
Thu, 24 Jul 2014 10:50:35 +0200 |
On Sun, Jul 20, 2014 at 9:47 PM, Emanuel Berg <embe8573@student.uu.se> wrote:
> But: is the stack of names global? Howcome all the Lisp
> don't get crazy from other Lisp changing "its"
> variables? But come to think of it, I very seldom use
> variables - I use `let's, and perhaps that is both a
> shield from interference and doesn't change the global
> name stack?
In dynamic scoping, each time you nest a let binding to same symbol
you add to this variable stack. When you take the value of a symbol
the value comes from the top of the stack of this symbol.
(defvar a 1)
(defun my-a ()
"Return current value of a (top top the stack)"
a)
(my-a)
=> 1
(let ((a 2)) ;; push 2 in the `a' stack
;; we usually say: a is shadowed in this scope
(my-a))
=> 2
;; at the end of let scope, 2 is popped from the stack
(my-a)
=> 1
(list
(my-a)
(let ((a 2))
(my-a))
(let ((a 3))
(let ((a 4))
(my-a)))
(my-a))
=> (1 2 4 1)