dynamic-scope

Is there a way to declare that a function should use the scope of the caller?

一笑奈何 提交于 2020-08-26 13:47:27
问题 is there a feautre similar to C macros which lets you reuse code in an inline manner, without creating a seperate scope for that piece of code? for example: a=3 def foo(): a=4 foo() print a will print 3, however i want it to print 4. i am aware of solutions involving objects like classes or a global dict, however i'm looking for a more primitive solution (like a function decorator for example) that would simply let me make changes inside the scope of the caller instead. thank you very much

Dynamic scoping questions in R

和自甴很熟 提交于 2020-01-22 15:08:45
问题 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 = 3 subset2(df, a < condition) Then I got the following error message: Error in eval(substitute(condition), df) : object 'a' not found I read the explanation as follows but don't quite understand: If eval() can’t find the variable inside the data frame (its

Lexical scoping vs dynamic scoping

[亡魂溺海] 提交于 2019-12-29 06:25:00
问题 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

Dynamic Scoping - Deep Binding vs Shallow Binding

孤街浪徒 提交于 2019-12-29 04:29:09
问题 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) 回答1: Deep binding binds the

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

蹲街弑〆低调 提交于 2019-12-27 10:55:01
问题 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 non-local variables accessed from within a function are resolved to variables present in the calling scope of that function when it is called. x=1 function g () { echo $x ; x=2 ; } function f () { local x=3 ; g ; } f # does this print 1, or 3? echo $x #

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

Deadly 提交于 2019-12-09 15:55:42
问题 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 is a need for binding at all as every program I have written have been without them and I can find ways to write the examples in the conventional way - which I find more understandable. Are there examples of projects/programming paradigms that

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

喜夏-厌秋 提交于 2019-12-05 15:23:00
问题 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 diagram. Each time a function is applied, a new frame is created (labeled by E1 through E4 ) which represents a set of bindings between symbols and values. When a symbol is not bound in a frame, that frame's enclosing environment is queried for a binding of

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

时光总嘲笑我的痴心妄想 提交于 2019-12-04 03:59:06
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 is a need for binding at all as every program I have written have been without them and I can find ways to write the examples in the conventional way - which I find more understandable. Are there examples of projects/programming paradigms that make used of this? for example... in the animal speak example, you can get a similar effect with: (def

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

落花浮王杯 提交于 2019-12-03 17:15:05
问题 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 (* previous-power-of-two 2))))) >> (funcall power-of-two) 2 >> (funcall power-of-two) 4 >> (funcall power-of-two) 8 The function won't work in Emacs Lisp because of its dynamic binding behavior. I wonder if it is possible to implement the same function in Emacs Lisp without introducing

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

左心房为你撑大大i 提交于 2019-12-03 16:30:14
问题 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