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
3
votes
3 answers

Subtlety about Common Lisp scoping (dynamic vs lexical)

After reading documentation about the declaration SPECIAL, the special operator LET, the macro DEFVAR, and several questions here at StackOverflow about the dynamic versus lexical scoping in Common Lisp, as, for instance, this, I still can't…
Paulo Tomé
  • 1,910
  • 3
  • 18
  • 27
3
votes
5 answers

Is there a better way to simulate pointers in JavaScript?

I'm using dynamic scoping to simulate pointers in JavaScript as follows: var ptr = (function () { var ptr = "(" + String(function (value) { if (value === void 0) return upvalue; else upvalue = value; }) + ")"; return function…
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
2
votes
3 answers

WITH-OUTPUT-TO-STRING with multithreading in Common Lisp

I want to do something that means the following: (with-output-to-string (*standard-output*) (bt:join-thread (bt:make-thread (lambda () (format *standard-output* "Hello World"))))) ;=> "" (actual output) ;=> "Hello World" (expected…
digikar
  • 576
  • 5
  • 13
2
votes
1 answer

What is the output when you use shallow binding?

x : integer := 3 //global scope y : integer := 4 //global scope procedure add x := x + y procedure second(P : procedure) x : integer := 5 P() procedure first y : integer := 6 second(add) first() …
2
votes
3 answers

Lexical vs Dynamic interpreter in Scheme language

I still do not understand how a dynamic interpreter differ from a lexical one. I am working on scheme and i find it very difficult to know how a simple code like these one works dynamically and lexically. (define mystery (let ((x 2018)) …
2
votes
1 answer

scala: what is the inner class method for dynamic scoping?

i'm trying to evaluate all 3 methods of dynamic scoping described here (https://wiki.scala-lang.org/display/SYGN/Dynamic-scope) and i understand all but the "inner class method". it is described as follows: It is possible to achieve a similar…
Heinrich Schmetterling
  • 6,614
  • 11
  • 40
  • 56
2
votes
2 answers

Should code with trampoline and Y combinator work in lisp with dynamic scope?

I have lisp in javascript which is similar to scheme. It can be used with lexical and dynamic scopes. I was not sure how dynamic scope works and it's seems ok but this code don't work when scope is dynamic: (define Y (lambda (h) ((lambda…
jcubic
  • 61,973
  • 54
  • 229
  • 402
2
votes
4 answers

temporarily overwrite a globally defined function in Scheme let block?

suppose I have the following functions: (define (g x) (f x)) (define (f x) (+ 1 x)) I would like to temporarily call g with a different f. For example, something like this: (let ((f (lambda (x) (+ 2 x)))) (g 5)) I would like the code above to…
xdavidliu
  • 2,411
  • 14
  • 33
2
votes
1 answer

Get reference to a namespace where the function was called

Is there a general way in Python 3 to get a reference to the module's namespace where the function was called? The problem arises from the fact that global in function references to the namespace where the function was defined, but I want to obtain…
godaygo
  • 2,215
  • 2
  • 16
  • 33
2
votes
1 answer

Can you explain this LISP function and why issues arise with dynamic vs.lexical scoping?

While reading up on some lisp history here From LISP 1 to LISP 1.5, I came across this function: (define (testr x p f u) (if (p x) (f x) (if (atom? x) (u) (testr (cdr x) p f …
firephyz96
  • 263
  • 2
  • 6
2
votes
1 answer

How do I test a dynamic variable for existence?

As I understand, Dynamic Variables are looked up at runtime. I want to use them to enable parametrization similar to racket parameters. To do so, I have to set a default that should be overridable, but not necessarily changable. My current approach…
danielschemmel
  • 10,885
  • 1
  • 36
  • 58
2
votes
2 answers

Why are arguments to an enclosing function not captured by closures in Common Lisp?

test.lisp: (defvar test #'(lambda (var1) #'(lambda (var2) `((var1 . ,var1) (var2 . ,var2))))) (defvar var1 'wrong) (defvar var2 'wrong) And in the REPL: $ clisp -q -i test.lisp ;; Loading file test.lisp ... ;; Loaded…
Matt
  • 21,026
  • 18
  • 63
  • 115
2
votes
2 answers

Dynamic scope in macros

Is there a clean way of implementing dynamic scope that will "reach" into macro calls? Perhaps more importantly, even if there is, should it be avoided? Here's what I'm seeing in a REPL: user> (def ^:dynamic *a* nil) > #'user/*a* user> (defn f-get-a…
Omri Bernstein
  • 1,893
  • 1
  • 13
  • 17
2
votes
5 answers

Local variable to external function

I'm trying to access a variable local to a function in an external function as a free variable. This is what I'm trying to achieve: function try_evaluate() { var i = 0; show_r("i <= 10"); } function show_r(expression) { if…
Samuel BR
  • 51
  • 1
2
votes
1 answer

Does Eval really introduce dynamic scoping to JavaScript?

People say that Eval brings dynamic scope into JavaScript, but I don't see how that statement is valid. Using Eval evaluates the expression using the same lexical environment/variable environment as the calling environment (reference ECMA 262 v.…
contactmatt
  • 18,116
  • 40
  • 128
  • 186