Questions tagged [dynamic-scope]

Dynamic scoping is a method of variable scoping where variables are bound based upon the call stack, instead of where they are defined.

Dynamic scoping is a method for determining variable scope which uses the call-stack to figure out which value an unbound variable (a variable which is defined outside the currently executing function) should have. Dynamically scoped languages will look up the call stack (first the caller, than the caller's caller, and so on) to find unbound variables.


The more popular method of scoping, lexical scoping (alternatively, static scoping), uses the program source code to figure out how to place scopes. For example, consider the following Python code:

def increment_by(x):
    def inner(y):
        return x + y
    return inner

def main():
    add_two = increment_by(2)
    x = 12
    print(add_two(12))

main()

As output, this program will produce the number 14; this is because, when Python sees the unbound name called x, it looks to see how it was defined in the function increment_by, since inner was defined inside increment_by in the program source code. When increment_by was executed to produce the inner function, x was equal to 2, so Python uses the value 2 for the variable x inside inner.


However, the language Emacs Lisp is dynamically scoped, unlike Python - the equivalent code in Elisp is:

(defun increment-by (x)
    ;; This is actually the function called inner, but without a name
    (lambda (y) (+ x y)))

(defun main ()
    (let ((add-two (increment-by 2))
             (x 12))
            (funcall add-two 10)))

(main)

When run inside Emacs, the above code will return the value 22; this is because, when Elisp sees the unbound name called x, it will look to see what value the caller gave x. Since main calls increment-by, Elisp sees that main defined x to be 12, and thus uses the value 12 for x.


Typically, this tag is meant to denote questions about dynamic scoping and how it affects programming in dynamically scoped languages.

83 questions
32
votes
10 answers

What are the advantages of dynamic scoping?

I've learned that static scoping is the only sane way to do things, and that dynamic scoping is the tool of the devil, and results only from poor implementations of interpreters/compilers. Then I saw this snippet from a Common Lisp vs. Scheme…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
32
votes
1 answer

Clojure: binding vs. with-redefs

clojure.core has the macros bindings and with-redefs. Looking at the docstrings and the examples on clojuredocs.org, they seem to do something very similar. What is the difference and which one should I use in which situations?
Miikka
  • 4,573
  • 34
  • 47
30
votes
3 answers

Dynamic Scoping - Deep Binding vs Shallow Binding

I've been trying to get my head around shallow binding and deep binding, wikipedia doesn't do a good job of explaining it properly. Say I have the following code, what would the output be if the language uses dynamic scoping with a) deep binding b)…
John Jiang
  • 11,069
  • 12
  • 51
  • 60
28
votes
1 answer

clojure and ^:dynamic

I tried to understand dynamic variables and binding function so I tried this (clojure 1.3): user=> (defn f [] (def ^:dynamic x 5) (defn g [] (println x)) (defn h [] (binding [x 3] (g))) …
Kevin
  • 24,871
  • 19
  • 102
  • 158
28
votes
7 answers

Is it possible to achieve dynamic scoping in JavaScript without resorting to eval?

JavaScript has lexical scoping which means that non-local variables accessed from within a function are resolved to variables present in the parents' scope of that function when it was defined. This is in contrast to dynamic scoping in which…
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
18
votes
2 answers

What are the new rules for variable scoping in Emacs 24?

Emacs 24 now has lexically-scoped variables. It also still has dynamically-scoped variables, of course. Now that it has both, I'm quite confused about when a variable will have which kind of scope. There's a lexical-binding variable that controls…
Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159
16
votes
4 answers

How to create dynamical scoped variables in Python?

I am translating some code from lisp to Python. In lisp, you can have a let construct with the variables introduced declared as special and thus having dynamic scope. (See http://en.wikipedia.org/wiki/Dynamic_scope#Dynamic_scoping) How can I do…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
11
votes
4 answers

How to overcome the lack of local variable for emacs lisp closure

I'm now studying Emacs Lisp from the reference manual and Common Lisp from a LISP Book. from the Common Lisp book >> (setf power-of-two (let ((previous-power-of-two 1)) #'(lambda () (setf previous-power-of-two (*…
Sake
  • 4,033
  • 6
  • 33
  • 37
11
votes
1 answer

Lexical vs dynamic scoping in terms of SICP's Environment Model of Evaluation

In Section 3.2.2 of SICP the execution of the following piece of code (define (square x) (* x x)) (define (sum-of-squares x y) (+ (square x) (square y))) (define (f a) (sum-of-squares (+ a 1) (* a 2))) (f 5) is explained in terms of this…
ddk
  • 1,813
  • 1
  • 15
  • 18
10
votes
1 answer

Common Lisp Binding in Loop Macro

I want to rebind a special variable inside of a loop. Now, normally, this is accomplished using a let. (let ((*read-eval* nil)) (do-something-here)) But since the loop macro has these nice with clauses, I thought I might be able to do so in…
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
9
votes
2 answers

Closures and dynamic scope?

I think I understand why there is a danger in allowing closures in a language using dynamic scope. That is, it seems you will be able to close the variable OK, but when trying to read it you will only get the value at the top of global stack. This…
Eli Schneider
  • 4,903
  • 3
  • 28
  • 50
9
votes
2 answers

what is the practical purpose of clojure's dynamic vars and binding?

I had a look at the references: http://clojure.org/vars#Vars%20and%20the%20Global%20Environment, http://clojuredocs.org/clojure_core/clojure.core/binding as well as clojure and ^:dynamic and Clojure Dynamic Binding I still don't understand why there…
zcaudate
  • 13,998
  • 7
  • 64
  • 124
8
votes
2 answers

Clojure Dynamic Binding

I realize the following is a bad idea for many reasons. I also realize that given I have a stackoverflow rep of 23, it's nature to assume that I'm a newb learning to program. However, please humor me, and focus on the "how can we do this" rather…
user1647794
7
votes
2 answers

Why doesn't temp work in Perl 6 core settings?

I was looking in and saw this comment in the indir implementation: sub indir(Str() $path, $what, :$test = ) { my $newCWD := $*CWD.chdir($path,:$test); $newCWD // $newCWD.throw; { my $*CWD = $newCWD; # temp doesn't work in…
brian d foy
  • 129,424
  • 31
  • 207
  • 592
7
votes
2 answers

strange interaction between lexical-binding and defvar in emacs lisp

The following emacs lisp file is about seeing what happens when Alice uses a lexically bound local variable foo in her init file and Bob defines foo as a global special variable with defvar in his init file and Alice borrows part of Bob's init file…
Jisang Yoo
  • 3,670
  • 20
  • 31
1
2 3 4 5 6