- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在阅读 this paper ( ungated copy )关于评估 R 编程语言的设计,并且无法理解有关词法范围(或没有词法范围)的特定示例。
在第 4 页,作者提供了以下使用 with
的示例。功能:
with(formaldehyde, carb*optden)
The astute reader will have noticed that the above example clashes with our claim that R is lexically scoped. As is often the case, R is lexically scoped up to the point it is not. R is above all a dynamic language with full reflective access to the running program’s data and representation. In the above example, the implementation of with sidesteps lexical scoping by reflectively manipulating the environment. This is done by a combination of lazy evaluation, dynamic name lookup, and the ability turn code into text and back:
with.default <- function(env, expr, ...)
eval(substitute(expr),env, enclose=parent.frame())
The function uses
substitute
to retrieve the unevaluated parse tree of its second argument, then evaluates it witheval
in the environment constituted by composing the first argument with the lexically enclosing environment. The ‘...
’ is used to discard any additional arguments.
with
的使用情况如何在这种情况下函数是否违反了词法作用域的原则?
最佳答案
通常在 R 词法范围的上下文中讨论时,意味着在函数的父环境中查找函数中的自由变量(即在函数中使用但未在函数中定义的变量),而不是在调用者(也称为父框架),但 with.default
中没有自由变量所以这个例子并没有说明在这个意义上违反了词法作用域。
例如,这说明了词法作用域:
x <- 1
f <- function() x
g <- function() { x <- 0; f() }
g() # 1
f
的环境中定义的。定义在。如果 R 使用动态作用域而不是词法作用域,答案将是 0(使用调用者的环境)。我们可以说明 R 如何模拟动态作用域,如下所示:
f <- function() eval.parent(quote(x))
g() # 0
with.default
的第二个实际参数。没有在词汇上进行评估,这种解释似乎很可能。
with.default
的第二个实际参数不是相对于周围的词法环境进行评估的。读入
with.default
作为表达式使用
substitute
然后使用
eval
相对于第一个参数进行评估.有一些关于词法作用域的定义应该是什么的问题,因为即使在广泛讨论时也很少定义它,但与 R 相关的典型讨论将其称为对自由变量的处理。参见示例
Gentleman & Ihaka .
关于r - 理解 R 中的词法作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20766649/
我试图了解传递给 setTimeout 的箭头函数如何记住上一个执行上下文中的 this 的值。我知道在执行箭头函数时会使用词法作用域规则查找 this 值。这是否意味着箭头函数关闭变量和 this
这个问题已经有答案了: How does the "this" keyword in Javascript act within an object literal? [duplicate] (4 个
我已阅读 this问题,我想我已经理解了投票最多的答案,但他说 since basically every programming language in wide use today uses le
如何让这段宏发挥预期的作用? -- 我想从词法环境中捕获 p 而不必将其作为参数发送给宏。 (define-syntax-rule (fi a b) (if p a b)) ;--->capt
Program A() { x, y, z: integer; procedure B() { y: integer; y=0;
我正在用 Java 实现自己的链表。节点类只有一个名为“name”的字符串字段和一个名为“link”的节点。现在我有一个测试驱动程序类,它只按顺序插入几个名字。现在,我正在尝试编写一种排序方法来按字母
考虑到这个question SO,其中调用了整个 C# 内存中编译器。只有lexical and syntactic analyzing时是必需的:将文本解析为词素流,检查它们并退出。 在System
我有 2 个场景。 这失败了: class F { public X X { get; set; } } 错误 CS0102:类型“F” ' 已经包含 ' X 的定义| ' 这个有效: class
我有一个用 NodeJS 执行的 .js 文件。这是我的文件的内容: var ctry = "America"; function outer(msg) { console.log(msg +
我对编写汇编程序的概念非常陌生,即使在阅读了大量 Material 之后,我仍然很难理解几个概念。 将源文件实际分解为 token 的过程是什么?我相信这个过程称为词法分析,我已经到处搜索有意义的真实
在 static scoping,标识符可以通过分析/解析源代码来确定(与动态作用域不同,动态作用域或多或少需要了解调用者环境)。 我的问题是这样的,因为静态作用域只需要解析源代码以了解作用域和标识符
编辑:我在第一个答案后更改了示例代码,因为我想出了一个简单的版本来回避相同的问题。 我目前正在学习 Common Lisp 的作用域属性。在我认为我有一个坚实的理解之后,我决定编写一些我可以预测结果的
考虑这段代码: class Bar(object): pass class Foo(object): def bar(self): return Bar() f = Foo() def Bar
将 ES6 箭头函数与词法 this 绑定(bind)结合使用非常棒。 但是,我刚才在使用典型的 jQuery 单击绑定(bind)时遇到了一个问题: class Game { foo() {
将 ES6 箭头函数与词法 this 绑定(bind)结合使用非常好。 但是,我刚才在将它与典型的 jQuery 点击绑定(bind)一起使用时遇到了一个问题: class Game { foo(
我是一名优秀的程序员,十分优秀!