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
3
votes
2 answers

Ruby Koans - Continuation of Lexical Scope vs Inheritance Hierarchy

I've had a chance to look around in StackOverflow and found this same question which I was trying to better understand from Ruby Koans (Ruby Koans: explicit scoping on a class definition part 2). class MyAnimals LEGS = 2 class Bird < Animal …
wmock
  • 5,382
  • 4
  • 40
  • 62
3
votes
2 answers

Racket: lexical scope inside for

In Haskell, inside a list comprehension, i can bind expressions to a variable every iteration: [a | x <- xs, let a = x ^ 2, a >= 25] How do i bind lexical variables in Racket's for comprehension? Currently i have this code: (define (euler4) …
Mirzhan Irkegulov
  • 17,660
  • 12
  • 105
  • 166
3
votes
1 answer

Ambiguous variation of default environment in `getClasses()` (standard function vs. formal S4 method)

I'm having some trouble figuring out what's exactly going on here with respect to "environment nesting"/lexical scoping: The problem The default value of argument where in function getClasses() seems to vary depending on whether getClasses() is…
Rappster
  • 12,762
  • 7
  • 71
  • 120
2
votes
5 answers

Why are lexical scopes prefered by the compilers?

How does lexical scope help the compilers? Does it help in compilation or optimization?
unj2
  • 52,135
  • 87
  • 247
  • 375
2
votes
3 answers

JS Scoping issue

Consider the following piece of code: function processParagraph(paragraph) { if (paragraph.charAt(0) === '%') { for (var level = 0; paragraph.charAt(level) === '%'; level++) {} return { type: 'h' + level, …
helpermethod
  • 59,493
  • 71
  • 188
  • 276
2
votes
3 answers

In Ruby, how does one add to an object a method with access to variables in outer scope?

I'm new to Ruby. I'm at the stage where I'm unsuccessfully attempting to write things in Ruby as I would in some other language. I'm trying to add a method to an object – a humble array, let's say. Not to all arrays, just to one specific one. This…
davidchambers
  • 23,918
  • 16
  • 76
  • 105
2
votes
2 answers

JavaScript Closure changing variable value from the outer scope

I don't understand why my returned function didn't change the value of the variable from the outer function. Hi, I wrote js function: function num() { let number = 0; console.log(number) return function() { return number++ …
2
votes
2 answers

Rebinding a Subset of Lexical Variables

Given a set of lexical variables, is it feasible to rebind a subset of them depending on circumstances at runtime. My first idea was to use #'set something like: (let (A B C D E) (declare (ignorable A B C D E)) (mapc #'set '(b e) (list 1 2)) …
davypough
  • 1,847
  • 11
  • 21
2
votes
1 answer

Why does a Dart program behave differently depending on where variables are declared when using lexical scope?

I ran into what seemed like very strange behavior from Dart today. There must be something wrong with how I am understanding local variables and lexical scope. Let's get into some code. The context is a testing grade conversion from numbers to…
Paul Gestwicki
  • 1,610
  • 1
  • 15
  • 18
2
votes
3 answers

Lexical vs Dynamic interpreter in Scheme language

I still do not understand how a dynamic interpreter differ from a lexical one. I am working on scheme and i find it very difficult to know how a simple code like these one works dynamically and lexically. (define mystery (let ((x 2018)) …
2
votes
1 answer

Understanding scoping of nested functions

I'm attempting to refactor a script by splitting it into multiple functions, having a main function and "help functions". Here I stumbled upon a problem which can be reduced to the following example: g <- function(a,b){ # help function a^2…
maryam
  • 101
  • 5
2
votes
2 answers

Is there a downside to using closures over prototypes?

I like the general style of using closures to create objects with private properties. What I'm unsure of is whether it's more efficient to create the prototype methods within a closure or on the actual prototype of the object. Consider the following…
Frank
  • 2,050
  • 6
  • 22
  • 40
2
votes
1 answer

JS through2 Arrow function this.push()

I have a problem with lexical binding in JavaScript. This is my code: .pipe(through.obj((url, enc, done)=>{ if(!url) return done(); request.head(url, (err, response)=>{ this.push(url + ' is ' + (err ? 'down' : 'up') +…
백승훈
  • 31
  • 3
2
votes
2 answers

Should code with trampoline and Y combinator work in lisp with dynamic scope?

I have lisp in javascript which is similar to scheme. It can be used with lexical and dynamic scopes. I was not sure how dynamic scope works and it's seems ok but this code don't work when scope is dynamic: (define Y (lambda (h) ((lambda…
jcubic
  • 61,973
  • 54
  • 229
  • 402
2
votes
2 answers

SICP exercise 3.20 - understand the envrionmental diagram (missing binding in my diagram)

There was a question regarding this exercise in this forum, but it dose not answer my specific question. This exercise asks to draw the environmental diagrams for (define x (cons 1 2)) (define z (cons x x)) (set-car! (cdr z) 17) (car x) where cons,…
englealuze
  • 1,445
  • 12
  • 19