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
4
votes
1 answer

Elisp: Symbol's value as variable is void with let* and (lambda)

Disclaimer: I started to hack around with elisp today. I am really wondering what I am getting the following error: Symbol's value as variable is void: response with the following code: (let* ((response (cons 'dict nil))) (nrepl-request:eval …
Neoasimov
  • 1,111
  • 2
  • 10
  • 17
4
votes
4 answers

In Perl, do "$a" and "$b" have any special use outside of the sort() function?

I asked a question about the use of "$a" and "$b" in Perl's sort() function the other day: What exactly are "$a" and "$b" in Perl's "sort()" function? I now have a follow up question. Are "$a" and "$b" only used by sort() or is there any other Perl…
tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
4
votes
1 answer

Lexical scoping in C# lambda/anonymous delegates

I want to check whether a simple mathematical expression would overflow (using checked and catch(OverflowException)), but without the need to use a try-catch block every time. So the expression (not the result!) should be passed to a function…
AndiDog
  • 68,631
  • 21
  • 159
  • 205
4
votes
1 answer

What's the logic behind this python global scoping magic?

I was messing around with the scoping in python and found something that I think is rather strange: g = 5 def foo(a): if a: global g g = 10 else: g = 20 print("global g: ",g) foo(False) print("global g: ",g) #…
monoceres
  • 4,722
  • 4
  • 38
  • 63
4
votes
2 answers

Does emacs lisp support lexically redefining a function?

Recent versions of Emacs support lexical binding for variables in elisp code. Is it also possible to lexically redefine functions? In other words, does Emacs Lisp have something like lexical-flet?
Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159
3
votes
1 answer

Does the inner function know that a variable is inside the temporal dead zone before searching further through the scope chain?

function b() { function a() { console.log(x); } a(); const x = 10; } const x = 20; b() If I understand lexical scoping and execution context correctly, when function a() is invoked, it should have a reference to b's…
james
  • 550
  • 8
  • 14
3
votes
0 answers

R package to extract minimal code required to reproduce a result

A trivial example. Let's say this is the original code: x = 1 y = 2 z = x * 3 If we specify that we want to reproduce z, then the package would return a new script containing only the lines of code required to generate the value z. x = 1 z = x *…
D Greenwood
  • 415
  • 2
  • 11
3
votes
1 answer

Why does using a variable in a function change the value returned in R when using superassignment (<<-) operator?

Why do bar and baz behave differently? When bar is called both the value of a printed and the value of a in the global scope are the same, but when baz is called the printed value and the value in the global scope are different. Seemingly, the only…
seaFan
  • 116
  • 5
3
votes
1 answer

Perl replace value without updating array

I have the following Perl script. #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @fruits = qw(apple banana orange pear); print Dumper \@fruits; foreach my $fruit (@fruits) { $fruit =~ s|apple|peach|; } print Dumper…
JeremyCanfield
  • 633
  • 11
  • 24
3
votes
2 answers

Struggling with lexical scoping and for loops

How do I get the modify_attr() function (below) not to capture/update the value of b in the following for loop? (simplified version, occurring within a mainloop()): for b in range(x): button = tk.Button(button_frame, text="<", command=lambda:…
Nathan
  • 118
  • 6
3
votes
1 answer

R - Checking if a string is a valid mathematical expression using non-standard evaluation

I would like to check if the strings below are valid mathematical expressions: s1 = 'sin(x)' s2 = 'sin(x*m)' s3 = 'sin' s4 = 'sin(xm)' By 'valid', I mean the expression is a combination of operators (must be used in conjunction with variables or…
user51462
  • 1,658
  • 2
  • 13
  • 41
3
votes
1 answer

The code outputs a number instead of undefined?

According to my understanding, the code below should output undefined when I call hi() but instead it logs a number. I'm using chrome. Can somebody elaborate why is this happening? Thanks in advance. Screen shot of Code: const obj = { name:…
aaKhan
  • 247
  • 2
  • 16
3
votes
2 answers

Why does jQuery has a "window=this" at the very begining and say it would speed up references to window?

When I open jQuery's source code I find this line. var // Will speed up references to window, and allows munging its name. window = this Why and how this line will speed up?
David
  • 133
  • 5
3
votes
2 answers

difference between let inside and outside a lambda expression

Consider a module with the following procedures: (define-module (test test) #:export (proc1 proc2 proc3)) (define proc1 (let ((module (current-module))) (lambda () (format #t "~s\n" module)))) (define proc2 (lambda () (let…
Ernest A
  • 7,526
  • 8
  • 34
  • 40
3
votes
1 answer

Understanding the environment model of evaluation

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…
lightning_missile
  • 2,821
  • 5
  • 30
  • 58