lexical-scope

How is Lexical Scoping implemented? [closed]

风格不统一 提交于 2019-12-02 20:55:56
A couple of years ago I started writing an interpreter for a little Domain Specific Language which included programmer-defined functions. At first I implemented variable scope using a simple stack of symbol-tables. But now I want to move to proper lexical scoping (with the option of closures). Can anyone explain the data-structure and algorithm behind lexical scope? To get correct lexical scoping and closures in an interpreter, all you need to do is follow these rules: In your interpreter, variables are always looked up in an environment table passed in by the caller or kept as a variable ,

Lexical scoping in a for loop enclosing a promise?

半世苍凉 提交于 2019-12-02 12:44:18
I have an ids object, which maps id strings to product objects. for id of ids product = ids[id] console.log product # Prints out something different each loop. :) Product.create(product).then -> console.log product # Only prints out the last id each loop. :( I'm using a library for database interactions, which exposes promises (indicated by the then function above). I'm trying to print out the product variable inside the then function, but I only seem to be getting the last id in ids , so it looks like it's a scoping issue. How can I scope the product variable properly so that it prints out a

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

Understanding the environment model of evaluation

冷暖自知 提交于 2019-11-29 16:11:25
Exercise 3.20 in SICP: Draw environment diagrams to illustrate the evaluation of the sequence of expressions (define x (cons 1 2)) (define z (cons x x)) (set-car! (cdr z) 17) (car x) 17 using the procedural implementation of pairs given above. My eyes are destroyed so I cannot draw. I will instead try to imagine as best as I can how the environment model evolves. First, here is the procedural pairs implementation. (define (cons x y) (define (set-x! v) (set! x v)) (define (set-y! v) (set! y v)) (define (dispatch m) (cond ((eq? m 'car) x) ((eq? m 'cdr) y) ((eq? m 'set-car!) set-x!) ((eq? m 'set

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 (

Closures in Python

点点圈 提交于 2019-11-29 04:31:35
I've been trying to learn Python, and while I'm enthusiastic about using closures in Python, I've been having trouble getting some code to work properly: def memoize(fn): def get(key): return (False,) def vset(key, value): global get oldget = get def newget(ky): if key==ky: return (True, value) return oldget(ky) get = newget def mfun(*args): cache = get(args) if (cache[0]): return cache[1] val = apply(fn, args) vset(args, val) return val return mfun def fib(x): if x<2: return x return fib(x-1)+fib(x-2) def fibm(x): if x<2: return x return fibm(x-1)+fibm(x-2) fibm = memoize(fibm) Basically,

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

What is the meaning of the dollar sign “$” in R function()?

我与影子孤独终老i 提交于 2019-11-28 09:53:10
Through learning R , I just came across the following code explained here . open.account <- function(total) { list( deposit = function(amount) { if(amount <= 0) stop("Deposits must be positive!\n") total <<- total + amount cat(amount, "deposited. Your balance is", total, "\n\n") }, withdraw = function(amount) { if(amount > total) stop("You don't have that much money!\n") total <<- total - amount cat(amount, "withdrawn. Your balance is", total, "\n\n") }, balance = function() { cat("Your balance is", total, "\n\n") } ) } ross <- open.account(100) robert <- open.account(200) ross$withdraw(30)

Ruby - Lexical scope vs Inheritance

早过忘川 提交于 2019-11-28 04:08:04
This is a continuation this original SO question: Using "::" instead of "module ..." for Ruby namespacing In the original SO question, here is the scenario presented which I'm still having trouble understanding: FOO = 123 module Foo FOO = 555 end module Foo class Bar def baz puts FOO end end end class Foo::Bar def glorf puts FOO end end puts Foo::Bar.new.baz # -> 555 puts Foo::Bar.new.glorf # -> 123 Can someone provide some explanation behind why the first call is returning 555 and why the second call is returning 123? You can think of each appearance of module Something , class Something or

Dynamic and Lexical variables in Common Lisp

老子叫甜甜 提交于 2019-11-28 03:56:17
I am reading the book 'Practical Common Lisp' by Peter Seibel. In Chapter 6, "Variables" sections "Lexical Variables and Closures" and "Dynamic, a.k.a. Special, Variables". http://www.gigamonkeys.com/book/variables.html My problem is that the examples in both sections show how (let ...) can shadow global variables and doesn't really tell the difference between the Dynamic and Lexical vars. I understand how closures work but I don't really get whats so special about let in this example: (defvar *x* 10) (defun foo () (format t "Before assignment~18tX: ~d~%" *x*) (setf *x* (+ 1 *x*)) (format t