了解LISP中的绑定和自由变量

了解LISP中的绑定和自由变量

本文介绍了了解LISP中的绑定和自由变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 SICP ,绑定和自由变量的主题已经出现.但是,我对此感到困惑.术语绑定变量"仅适用于形式参数的变量吗?另外,文本说过程定义绑定"了它的形式参数.这使我感到困惑,因为有人说我们将值绑定"到变量.显然,当我们谈论不同类型的变量时,该术语似乎意味着不同的含义.有人可以弄清绑定变量是什么,绑定的含义是什么?最后,与绑定变量相反,什么是自由变量?所有这些与范围有什么关系?

I'm reading SICP, and the topic of bound and free variables has come up. However, I am confused about it. Does the term "bound variables" only apply to variables that are formal parameters? In addition, the text says that a procedure definition "binds" its formal parameters. This confuses me with the fact that some people say that we "bind" a value to a variable. Obviously, the term seems to mean different things when we're talking about different types of variables. Could someone clear up what a bound variable is and what binding means? Finally, in contrast to a bound variable, what is free variable? How does all of this relate to scope?

推荐答案

只有两种类型的变量.全局和词汇.实际上,您可以将global视为词法范围的可变根,在这种情况下,只有一种类型的变量.

There are only two types of variables. Global and lexical. You can actually think of global as a changeable root of the lexical scope and in that case there is only one type of variables.

绑定变量是当前过程的形式参数,而其他任何全局变量或从以前的嵌套调用绑定的都是自由变量.

A bound variable is the formal parameters of the current procedure and everything else, which either is global or bound from previous nested calls, are free variables.

示例:

(lambda (x)
  (let ((y (+ x x))) ; + is free x is bound
    (+ x y)))        ; + and x is free, y is bound

请记住,let是ust语法糖,因此它实际上与此相同:

Remember let is ust syntactic sugar so it's really the same as this:

(lambda (x)
  ((lambda (y)
     (+ x y)) ; + and x is free, y is bound
   (+ x x)))  ; + is free x is bound

在内部lambda中,以y作为绑定变量的+x是自由的.在外部lambda中,绑定了x,而+是自由的. +可能是全局的.

In the inner lambda with y as bound variable + and x are free. In the outer lambda x is bound and + is free. + might be a global.

这篇关于了解LISP中的绑定和自由变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 14:14