简体   繁体   English

递归调用的变量是自由的还是有界的?

[英]Will recursively-called variable be free or bound?

I am trying to have a better understanding about free and bound variables.我试图更好地了解自由和绑定变量。 Here is an example code:这是一个示例代码:

(define (what-kind-of-var? guess x)
    (< (abs (- (square guess) x))
        0.001))

I see that bound variables here would be guess and x , and free variables < , abs , - , and square .我看到这里的绑定变量是guessx ,以及自由变量<abs-square What if I called what-kind-of-var?如果我调用what-kind-of-var? recursively?递归? Would it be a bound variable because it is binding itself?它会是一个绑定变量,因为它是绑定本身吗?

Thanks!谢谢!

  • guess and x are parameters. guessx是参数。 They're eventually bound (to respective arguments) when the function is applied.当应用函数时,它们最终被绑定(到各自的参数)。

  • < , abs , - , are actually bound to procedures in the initial environment. <abs- ,实际上绑定到初始环境中的过程。 So they're not free variables.所以它们不是自由变量。

  • square would be the free variable, subject to the fact that what-kind-of-var? square将是自由变量,受制于what-kind-of-var?变量what-kind-of-var? is not defined in its scope.未在其范围内定义。 (note that sqr is bound in the initial environment). (注意sqr绑定在初始环境中)。

  • what-kind-of-var? is also not unbound, even if it calls itself recursively (assuming recursion is implemented properly in the language).也不是未绑定的,即使它递归地调用自己(假设递归在语言中正确实现)。 (define (f param) body) can be seen as (define f (lambda (param) body) (define (f param) body)可以看成(define f (lambda (param) body)

It would, under dynamic binding, but Scheme has lexical scoping.它会在动态绑定下,但 Scheme 有词法范围。

But actually it is neither.但实际上两者都不是。 "Free" or "bound" comes from lambda calculus. “自由”或“绑定”来自 lambda 演算。 what-kind-of-var? is a top-level variable , naming that lambda expression,是一个顶级变量,命名该 lambda 表达式,

(define what-kind-of-var? 
  (lambda (guess x)                        ;; this
     (< (abs (- (square guess) x))
         0.001)))

but in lambda calculus expressions cannot be named.但在 lambda 演算中,表达式不能被命名。 The only way to call it recursively in lambda calculus is to use Y combinator:在 lambda 演算中递归调用它的唯一方法是使用Y组合器:

((Y (lambda (what-kind-of-var?)                ;; outer
      (lambda (guess x)                            ;; inner
         (if (< (abs (- (square guess) x))
                0.001)
           guess
           (what-kind-of-var? (+ 1 guess) x)))))
  4 25)

and now of course what-kind-of-var?现在当然what-kind-of-var? is bound inside that new lambda expression under Y .根据该新lambda表达式内势必Y It's free in the nested, inner lambda, but bound in the outer one.它在嵌套的内部 lambda 中是免费的,但在外部 lambda 中是绑定的。

You need to read a handbook of logic or lambda calculus, there is the origin of the concepts of variable.你需要阅读一本逻辑或 lambda 演算手册,这里有变量概念的起源。

When a variable is inside a function and that function takes as parameter that variable, it is bound, whatever the function is recursive or not.当一个变量在一个函数内并且该函数将该变量作为参数时,无论该函数是否递归,它都是绑定的。

The idea of binding means that a location of memory is allocated and the variable symbol denotes that location.绑定的思想意味着分配一个内存位置,变量符号表示该位置。 Not every bound location can be accessed by a variable.并非每个绑定位置都可以通过变量访问。

In the case of free variables, there are many ways to bind it to something (in C language some free variables are bound by the linking process and some are never bound).在自由变量的情况下,有很多方法可以将它绑定到某个东西(在 C 语言中,一些自由变量被链接过程绑定,而有些则从不绑定)。 In Lisp, there are many ways a free variable can be bound -- dynamic binding, static/scope binding, or in lisp_N , with N > 2 there are lots of different ways to bind a variable.在 Lisp 中,可以通过多种方式绑定自由变量——动态绑定、静态/范围绑定,或者在lisp_N 中,当N > 2 ,有很多不同的方式来绑定变量。 But whatever the implementation of a variable is, the origin of the concept comes from math logic.但无论变量的实现是什么,概念的起源都来自数学逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM