简体   繁体   中英

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)

"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 :

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 .

Now suppose we had renamed x to y throughout the definition:

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 . It doesn't matter whether it's named x or y .

In contrast, if we defined f as follows:

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 . It matters that we renamed z to w , because the occurrence of z in the function body was "free", not "bound".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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