Lexical scoping (sometimes known as static scoping ) is a convention used with many programming languages that sets the scope (range of functionality) of a variable so that it may only be called (referenced) from within the block of code in which it is defined. The scope is determined when the code is compiled. A variable declared in this fashion is sometimes called a private variable.
Questions tagged [lexical-scope]
271 questions
1
vote
2 answers
Setting function symbols lexically
I'm looking for a way to easily, temporarily swap functions out.
I know that I can manually set a function symbol like so:
CL-USER> (setf (symbol-function 'abcd) #'+)
#
CL-USER> (abcd 1 2 4)
7
I also know that labels or flet can…

BnMcGn
- 1,440
- 8
- 22
1
vote
0 answers
How does a free variable affect the attributes of a local variable in R?
I have a constructor function that creates a function. There is a variable named 'features' that is local to the constructor, and another variable named 'features' that is local to the function that is created by the constructor. These are…

data-dancer
- 33
- 6
1
vote
1 answer
calling objects in nested function R
First off, I'm an R beginner taking an R programming course at the moment. It is extremely lacking in teaching the fundamentals of R so I'm trying to learn myself via you wonderful contributors on Stack Overflow. I'm trying to figure out how nested…

user3653647
- 69
- 6
1
vote
1 answer
lexical scope in R
I recently learned that R has both lexical and dynamical scoping available, but that it uses lexical scope by default. The next case really confused me:
> x <- 1
> f <- function(y) { x + y }
> f(5) # we expect 6
[1] 6
> x <- 10
> f(5) #…

Godel
- 33
- 5
1
vote
2 answers
Why isn't this form evaluated inside the lexical context of the let form
I am trying to make a macro that creates a function that takes S-expresions and evaluates them inside the lexical context of the fixture. Here is the macro I wrote:
(defmacro def-fixture (name bindings)
"Return a function that takes the form to…

PuercoPop
- 6,707
- 4
- 30
- 40
1
vote
3 answers
Javascript - Closures - Lexical Scoping - How to include a loop variable data in nested function?
Possible Duplicate:
Javascript infamous Loop problem?
I have the following code:
function test() {
var columns = options.columns;
for (var i =0; i < columns.length; i++) {
if (columns[i].type === "number") {
var field =…

James
- 2,876
- 18
- 72
- 116
1
vote
1 answer
Defining multiple local functions with "let over lambda" form in Scheme
I was curious about defining multiple lexically scoped functions in Scheme that can call each other. Working in SICP, I produced the following function using block structure to solve Exercise 1.8 (calculating cube-root using Newton's…

dtg
- 1,803
- 4
- 30
- 44
1
vote
1 answer
emacs lexical scoping and quoted variable name
I was experimenting with interplay between Emacs lexical scoping (new feature of Emacs 24) and add-to-list and found the interplay confusing and I don't know how to make sense of it. Here is a minimal example, except I use set instead of…

Jisang Yoo
- 3,670
- 20
- 31
0
votes
1 answer
Understanding ls() scoping and environments
I'm having trouble understanding why this piece of R code works the way it does. Here is a simple function:
weird_ls <- function() {
some_env <- new.env()
assign("a", value = 1, envir = some_env)
assign("b", value = 2, envir = some_env)
…

weakCoder
- 11
- 3
0
votes
1 answer
Explain why is the function in closure behaving differently when the function is defined outside and called in closure
function outer() {
let xx = 0;
function inner(fun) {
console.log(fun());
fun();
return "ds";
}
return inner;
}
const x = outer();
function fun() {
xx++;
return xx;
}
const y = x(fun);
console.log(y);
here the function…

kamal joshi
- 21
- 2
0
votes
0 answers
Scope Injection or Workaround in JavaScript
I am working on a library and I have this code. I have the following pattern. Let's say there's a function executeWithinScope:
// example 1
function executeWithinScope(callback) {
// Globally defined variable
Tracker.currentScope = new Scope();…

Tim Nimets
- 348
- 2
- 9
0
votes
2 answers
Functions has access to it own lexical as well outer environment
Functions has access to it own lexical as well outer environment but it's not working or maybe I don't understand why it's working like that.
Here is the code
function makeArmy() {
let shooters = [];
let i = 0;
while (i < 10) {
…

Akhil Rana
- 145
- 2
- 12
0
votes
1 answer
ECMASCRIPT Closures - What is Evaluation block in JavaScript?
https://tc39.es/ecma262/multipage/ecmascript-language-statements-and-declarations.html#sec-block-runtime-semantics-evaluation
14.2.2 Runtime Semantics: Evaluation
Block : { }
Return empty.
Block : { StatementList }
Let oldEnv be the running…
user19551894
0
votes
0 answers
I can't understand why is the output is -96 of this code
I actually expect that output should be 101 during the first iteration and 3 during the second iteration but it's actually -96 for both iterations.
Here is the code
const arr = [];
for (var i = 100; i > 0; i = i - 99) {
arr.push(function () {
…

Volodya
- 3
- 1
0
votes
2 answers
what is the lexical scope of an object?
I've been reading a little bit into execution contexts and lexical scoping. From my understanding,
if a function is declared, it will have a reference to the lexical environment (LE) that defined that function. for example,
var x = 20;
function…

james
- 550
- 8
- 14