Product Design, Manufacturing & Innovation Resources
家 » R 中的词汇作用域

R 中的词汇作用域

1980
  • Gerald Jay Sussman
  • Guy L. Steele Jr.
Computer programmer demonstrating lexical scoping in R programming language.

(图片仅供参考)

R 使用词法作用域,这是从 Scheme 语言继承而来的概念。这意味着函数中自由变量的值是通过在函数定义环境中而不是调用环境中查找来解析的。这使得函数行为更加可预测,并且不受调用上下文的影响,这是函数式编程的一个关键特性。

Lexical scoping, also known as static scoping, is a fundamental convention for looking up variable names in a 编程语言. In R, when a function needs to access a variable that is not defined locally (a “free variable”), it searches for that variable in the environment where the function was created. This “creation environment” is permanently attached to the function. This contrasts with dynamic scoping, used in some older languages, where the search would proceed up the chain of function calls (the “calling environment”).

For example, consider a function `f()` that uses a variable `x` but doesn’t define it. If `f()` is defined in the global environment where `x` is 10, then no matter where `f()` is called from, it will always use `x = 10`. Even if it’s called from another function that has its own local `x = 20`, `f()` will ignore the calling environment’s `x` and use the one from its definition environment. This behavior makes code easier to debug and reason about, as a function’s behavior is determined solely by its own code and the environment in which it was defined.

This feature is crucial for functional programming paradigms, enabling powerful constructs like closures, where a function can “remember” the environment in which it was created. This is heavily utilized in advanced R programming and package development, such as in the `lapply` family of functions and in frameworks like Shiny for web applications.

UNESCO Nomenclature: 1203
– Computer science

类型

抽象系统

中断

Incremental

用法

广泛使用

前体

  • The concept of scope in programming languages
  • Lisp 编程语言及其方言
  • Scheme 编程语言,它普及了词法作用域
  • Lambda演算 as a theoretical foundation for functions and scope

应用程序

  • 在 R 中创建闭包用于函数式编程
  • 开发包命名空间以避免变量冲突
  • 启用 dplyr 等软件包中使用的非标准评估技术
  • 构建无副作用的健壮且模块化的代码

专利:

    潜在创新理念

    • 使用范围操作对不受信任的代码进行沙盒处理的安全 R 环境
    • 可视化任何函数的词法作用域链的调试工具
    • 一种使用闭包来表示状态的金融合约领域特定语言
    • 使用交互式范围探索来教授编程概念的教育工具
    • 自动管理跨节点变量范围的并行计算框架
    • 一个 准时制 编译器 for R that heavily optimizes based on static scope analysis
    • 用于创建具有嵌入数据和功能的自包含、可重现的分析对象的包
    • 通过捕获和恢复其定义环境来版本化功能行为的系统
    • 检测因误解范围规则而产生的潜在错误的 linter
    • 用于创建自定义、隔离的评估环境以进行测试的库
    Related to: lexical scoping, static scoping, functional programming, R, Scheme, environment, closure, free variable, namespace, non-standard evaluation.

    历史背景

    R 中的词汇作用域

    1973
    1980
    1980
    1980
    1980
    1986-01-01
    1990
    1970-01-01
    1975-06-01
    1980
    1980
    1980
    1982-07-01
    1988-06-01
    1990

    (如果日期未知或不相关,例如“流体力学”,则提供其显著出现的近似估计)

    第十年 周年纪念日

    只有注册会员才能免费获取完整尺寸的图片和下载内容。

    > 登录 <