简体   繁体   English

关于自由和绑定变量的问题

[英]questions on free and bound variables

I've been reading through Structures And Interpretations of Computer Programs and have come to a confusing point the book is trying to explain about free and bound variables. 我一直在阅读《计算机程序的结构和解释》,并得出一个令人困惑的观点,即本书试图解释自由变量和绑定变量。 I have read many explanations on this site about this concept but none seem to do it for me. 我已经在该站点上阅读了许多有关此概念的解释,但似乎没有一个对我有用。 Here is the text explaining bound and free variables: 这是解释绑定变量和自由变量的文本:

*the book uses the programming language Lisp (Scheme) *本书使用编程语言Lisp(Scheme)

"A formal procedure has a very special role in the procedure definition in that it doesn't matter what name the formal parameter has. Such a name is called a bound variable." “形式化过程在过程定义中具有非常特殊的作用,因为形式参数的名称无关紧要。这样的名称称为绑定变量。”

My first question is why is it important that it doesn't matter what name the formal parameter has? 我的第一个问题是,为什么形式参数的名称无关紧要,这很重要?

" We say that the procedure definition binds its formal parameters. The meaning of a procedure definition is unchanged if a bound variable is consistently renamed throughout the definition." “我们说过程定义绑定了它的形式参数。如果在整个定义中始终对绑定变量进行重命名,则过程定义的含义不变。”

ok, the use of the word binds here means absolutely nothing to me. 好的,在这里使用绑定一词对我来说绝对没有任何意义。 More importantly, what are some examples of a bound variable being renamed throughout a definition? 更重要的是,在整个定义中重命名绑定变量的一些示例是什么? Every procedure I've created throughout the book has never talked about changing the names of formal parameters of a procedure definition within that procedure definition. 我在整本书中创建的每个过程从未谈论过该过程定义中更改过程定义的形式参数名称。 From what I understand, you set the formal parameters at the beginning of the procedure definition and that is that. 据我了解,您可以在过程定义的开头设置形式参数,就是这样。

Let's say we defined a procedure f : 假设我们定义了一个过程f

def f(x):
    print(x + z)

One can describe it as follows: f takes a parameter x , and prints the result when x is added with the global variable z . 可以这样描述: f接受参数x ,并在x与全局变量z相加时打印结果。

Now suppose we had renamed x to y throughout the definition: 现在假设我们在整个定义中将x重命名为y

def f(y):
    print(y + z)

It's clear that this function does the exact same thing as it did before. 显然,此函数执行的功能与以前完全相同。 This is because the name x in the initial definition, or y in this definition, occurs "bound" in f . 这是因为初始定义中的名称x或此定义中的yf “绑定”。 It doesn't matter whether it's named x or y . 命名为x还是y都没有关系。

In contrast, if we defined f as follows: 相反,如果我们将f定义如下:

def f(x):
    print(x + w)

Now this definition of f is different: instead of adding its parameter with z , it adds it with w . 现在, f定义有所不同:与其将参数与z相加,而没有将其与w相加。 It matters that we renamed z to w , because the occurrence of z in the function body was "free", not "bound". 重要的是,将z重命名为w ,因为在函数体中z的出现是“自由的”,而不是“绑定的”。

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

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