dynamic-scope

How to create dynamical scoped variables in Python?

六眼飞鱼酱① 提交于 2019-12-03 12:06:07
问题 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 likewise in Python? It seems the language does not support this directly, if true, what would be a good way to emulate it? 回答1: I feel Justice is plain right in his reasoning here. On the other hand -- I can't resist implementing proof of concept for

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

馋奶兔 提交于 2019-12-03 05:52:15
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 when lexical binding is enabled, and I think I read something about defvar now declaring a dynamically-scoped variable, but in general I'm pretty lost. Is there a good explanation somewhere of Emacs 24's new scoping rules? Or put another way, when I look at a variable in Emacs Lisp code written for Emacs 24, how do I tell what scope that variable is

What are the advantages of dynamic scoping?

淺唱寂寞╮ 提交于 2019-11-30 06:35:14
问题 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 article: Both Lexically and Dynamically Lexical scope only, per the standard. scoped special vars. Common Dynamically scoped vars are provided Lisp just wins on this point. by some implementations as an extension but code using them is not portable. (I

clojure and ^:dynamic

∥☆過路亽.° 提交于 2019-11-30 04:50:17
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))) (h)) #'user/f user=> (f) 5 nil Confused, I tried this somewhat simpler code: user=> (def ^:dynamic y 5) #'user/y user=> (defn g [] (println y)) #'user/g user=> (defn h [] (binding [y 3] (g))) #'user/h user=> (h) 3 nil What is the difference between the two pieces of code? Why does the second example work but the first does not? Hint: I just realized that the following works (still don't fully understand why):

Lexical scoping vs dynamic scoping

被刻印的时光 ゝ 提交于 2019-11-29 04:38:32
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 that follows uses C syntax, but let's just treat it as pseudo-code. int a,b; int p() { int a, p; a = 0; b = 1; p = 2; return p; } void print() { printf("%d\n%d\n",a,b); } void q () { int b; a = 3; b = 4; print(); } main() { a = p(); q(); } Here is what I come up with. Using Dynamic scoping, the nonlocal references to a and b can change. So I have a=2 (

Clojure Dynamic Binding

纵饮孤独 提交于 2019-11-29 04:19:52
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 than "why do you want to do this / you don't want to do this" aspect. What I want: (def dog (Dog. ...)) (def cat (Cat. ...)) (with-animal dog (println (str "Dog: " (speak) "\n"))) (with-animal cat (println (str "Cat: " (speak) "\n"))) to output: Dog: woof Cat: meow So basically, I want with-animal to be a macro s.t. all occurences of the "speak"

clojure and ^:dynamic

落花浮王杯 提交于 2019-11-29 01:49:03
问题 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))) (h)) #'user/f user=> (f) 5 nil Confused, I tried this somewhat simpler code: user=> (def ^:dynamic y 5) #'user/y user=> (defn g [] (println y)) #'user/g user=> (defn h [] (binding [y 3] (g))) #'user/h user=> (h) 3 nil What is the difference between the two pieces of code? Why does the second example work but

Dynamic Scoping - Deep Binding vs Shallow Binding

北战南征 提交于 2019-11-28 22:41:59
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) shallow binding? x: integer := 1 y: integer := 2 procedure add x := x + y procedure second(P:procedure) x:integer := 2 P() procedure first y:integer := 3 second(add) ----main starts here--- first() write_integer(x) Deep binding binds the environment at the time the procedure is passed as an argument Shallow binding binds the environment at the time

What are the advantages of dynamic scoping?

有些话、适合烂在心里 提交于 2019-11-28 20:18:06
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 article: Both Lexically and Dynamically Lexical scope only, per the standard. scoped special vars. Common Dynamically scoped vars are provided Lisp just wins on this point. by some implementations as an extension but code using them is not portable. (I have heard the arguments about whether Dynamic scoping is or is not a Bad Idea in the first place. I don

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

走远了吗. 提交于 2019-11-28 03:54:45
问题 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: (invalid-function ((lambda (b) (lambda (a) (+ b a))) 3)) It appears to be telling me that ((lambda (b) (lambda (a) (+ b a))) 3) Is not a valid function. This seems wrong, because when I evaluate that expression I get (lambda (a) (+ b a)) which looks like a valid