Questions tagged [lexical-scope]

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.

271 questions
12
votes
2 answers

What are the distinctions between lexical and static scoping?

In R programing for those coming from other languages John Cook says that R uses lexical scoping while S-PLUS uses static scope. The difference can be subtle, particularly when using closures. I found this odd because I have always thought lexical…
efrey
  • 399
  • 1
  • 12
11
votes
1 answer

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

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…
ddk
  • 1,813
  • 1
  • 15
  • 18
10
votes
2 answers

Why are variables declared with "our" visible across files?

From the "our" perldoc: our has the same scoping rules as my, but does not necessarily create a variable. This means that variables declared with our should not be visible across files, because file is the largest lexical scope. But this is not…
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
8
votes
6 answers

Closures in Python

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): …
Kyle Cronin
  • 77,653
  • 43
  • 148
  • 164
8
votes
0 answers

Primitive function in R

Can someone give me explanation for the below sentence highlighted in Bold. "Primitive functions are only found in the base package, and since they operate at a low level, they can be more efficient (primitive replacement functions don’t have to…
Naveen Gabriel
  • 679
  • 2
  • 9
  • 25
8
votes
2 answers

Sharing a thread variable without making it global (Perl)

I'm trying to write a simple script that uses threads and shares a variable, but I don't want to make this variable global to the whole script. Below is a simplified example. use strict; use warnings; use threads; use threads::shared; my…
tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
8
votes
1 answer

When is Lexical Scope for a function within a function determined?

I've looked at the other lexical scoping questions in R and I can't find the answer. Consider this code: f <- function(x) { g <- function(y) { y + z } z <- 4 x + g(x) } f(3) f(3) will return an answer of 10. My question is why? At the…
Steve Rowe
  • 19,411
  • 9
  • 51
  • 82
8
votes
3 answers

Lisp warning: xx is neither declared nor bound, it will be treated as if it were declared SPECIAL

I am new to lisp and am writing a few simple programs to get more familiar with it. One of the things I am doing is writing a recursive and iterative version of a factorial method. However, I have come across a problem and can't seem to solve…
Aaron
  • 117
  • 1
  • 2
  • 7
8
votes
3 answers

How are Perl's lexically-scoped pragmas implemented?

Pragmas, like autodie, according to the docs, are lexically scoped. { use autodie; .. .. } # Can die here Does this apply to all modules loaded with use? As far as I know, use is almost the same as: BEGIN { require autodie; …
snoofkin
  • 8,725
  • 14
  • 49
  • 86
7
votes
3 answers

What are the rules for re-binding?

[NOTE: I asked this question based on an older version of Rakudo. As explained in the accepted answer, the confusing output was the result of Rakudo bugs, which have now been resolved. I've left the original version of the Q below for historical…
codesections
  • 8,900
  • 16
  • 50
7
votes
1 answer

Why are all of the classes in Rakudo's src/core/Int.pm declared with my?

Looking at the source for Int, I see that all of the classes are declared with my, which I would have thought would make them private and not available outside that file. But, they obviously are. Why do they need to be declared that way? my class…
brian d foy
  • 129,424
  • 31
  • 207
  • 592
7
votes
3 answers

How to translate 'this' in D3 JavaScript to TypeScript?

I know that 'this' in JavaScript has a different meaning than in in TypeScript, as per this article 'this' in TypeScript. I have the following code in JavaScript used to create a thicker stroke on the selected node, and give all other nodes a…
7
votes
2 answers

Why doesn't temp work in Perl 6 core settings?

I was looking in and saw this comment in the indir implementation: sub indir(Str() $path, $what, :$test = ) { my $newCWD := $*CWD.chdir($path,:$test); $newCWD // $newCWD.throw; { my $*CWD = $newCWD; # temp doesn't work in…
brian d foy
  • 129,424
  • 31
  • 207
  • 592
7
votes
2 answers

strange interaction between lexical-binding and defvar in emacs lisp

The following emacs lisp file is about seeing what happens when Alice uses a lexically bound local variable foo in her init file and Bob defines foo as a global special variable with defvar in his init file and Alice borrows part of Bob's init file…
Jisang Yoo
  • 3,670
  • 20
  • 31
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
1
2
3
18 19