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

Emacs lisp: why does this sexp cause an invalid-function error?

The sexp in question is (((lambda (b) (lambda (a) (+ b a))) 3) 5) which, to me, looks like it should evaluate to 8, and in other lisps (e.g. Racket) it does, but in elisp it instead throws this error: Debugger entered--Lisp error:…
James Porter
  • 1,853
  • 2
  • 17
  • 30
6
votes
2 answers

Lexical scoping vs dynamic scoping

So I have this problem where I have to figure out the output using two different scoping rules. I know the output using lexical scoping is a=3 and b=1, but I am having hard time figure out the output using dynamic scoping. Note:the code example…
Samson
  • 133
  • 2
  • 7
5
votes
1 answer

When is it appropriate to set a request-scoped variable in a JSP?

In my experience, it is rarely/never necessary to set scope="request" on an EL variable. For example, I have a page that, given an item parameter, constructs a URL specific to that item based on its properties. This page is included in any page that…
Spencer Small
  • 91
  • 1
  • 1
  • 5
5
votes
2 answers

Dynamic scoping questions in R

I'm reading the AdvancedR by Hadley and am testing the following code on this URL subset2 = function(df, condition){ condition_call = eval(substitute(condition),df ) df[condition_call,] } df = data.frame(a = 1:10, b = 2:11) condition =…
Jason
  • 1,200
  • 1
  • 10
  • 25
5
votes
1 answer

Emulating lisp cons cells in Tcl

A list in lisp is a series of cons cells, but in Tcl, a list is a string with whitespace separating the elements. For translating code from lisp to tcl, one might simply take lisp lists and translate them to Tcl lists. However, this runs into…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
5
votes
3 answers

Why aren't SAS Macro Variables Local-Scope by Default?

I found this very helpful SO page while trying to resolve an issue related to macro variable scope. why doesn't %let create a local macro variable? So to summarize, writing %let x = [];or %do x = [] %to []; in a macro will: create a local-scope…
Max Power
  • 8,265
  • 13
  • 50
  • 91
4
votes
1 answer

Lexical or Dynamic scope - Haskell implemented evaluator

My professor has given us a question after talking about the difference between lexical and dynamic scope. He presented us a simple evaluator (of a fictional language) coded in Haskell. The following is the code: type Var = ... type Env = ... data…
Yan Zhuang
  • 436
  • 3
  • 10
4
votes
1 answer

How to write a PicoLisp function that does not shadow variables with it's parameters

I am idly exploring PicoLisp, and find myself perplexed about how to write meta-programming functions that would traditionally be handled with macros (in other lisp dialects). The biggest source of concern for me is that I do not see how I can…
Charlim
  • 521
  • 4
  • 12
4
votes
3 answers

dealing with a Emacs Lisp dynamic scope pitfall in old days

In the old days, Emacs had no support for lexical scope. I am wondering how people dealt with a particular pitfall of dynamic scope in those days. Suppose Alice writes a command my-insert-stuff which relies on the fp-repeat function defined in fp.el…
Jisang Yoo
  • 3,670
  • 20
  • 31
4
votes
4 answers

Dynamic "Scoping" of C# Checked Expression

Is it possible (in C#) to cause a checked(...) expression to have dynamic "scope" for the overflow checking? In other words, in the following example: int add(int a, int b) { return a + b; } void test() { int max = int.MaxValue; int…
feralin
  • 3,268
  • 3
  • 21
  • 37
3
votes
2 answers

How to we do dynamic-scoping to implement transaction tracing in NodeJs?

I am building a client and a server side framework (NodeJs) in which I want to trace transactions. I have the ability to pass headers (transaction_id) between client and server, however, I want to be able to set the transaction_id…
ra9z
  • 73
  • 7
3
votes
4 answers

Static scoping in C/C++

In the following code, 2 is printed. int x = 1; int f(int y) { return x; } int main() { x = 2; printf("%d", f(0)); } How is it happening if we have static scoping in C? Why isn't 1 printed? Printing 2 in this case isn't a dynamic…
RoG
  • 411
  • 4
  • 20
3
votes
2 answers

passing model parameters to R's predict() function robustly

I am trying to use R to fit a linear model and make predictions. My model includes some constant side parameters that are not in the data frame. Here's a simplified version of what I'm doing: dat <- data.frame(x=1:5,y=3*(1:5)) b <- 1 mdl <-…
Paul
  • 3,321
  • 1
  • 33
  • 42
3
votes
4 answers

lisp: dynamic scope vs explicit parameter passing

I see two different patterns for "output" functions in (common) lisp: (defun implicit () (format t "Life? Don't talk to me about life!")) (defun explicit (stream) (format stream "This will all end in tears.")) (defun test-im-vs-ex-plicit () …
3
votes
2 answers

Why can't localize lexical variable in Perl?

I have below Perl code. use warnings; use strict; my $x = "global\n"; sub a { print $x; } sub b { local $x = "local\n"; a(); } a(); b(); a(); Even if $x has scope inside b() subroutine why Perl doesn't allows to localize it ?
thatisvivek
  • 875
  • 1
  • 12
  • 30