Understanding bound and free variables in LISP

I am reading SICP and the topic of bound and free variables came up. However, I am confused by this. Does the term "bound variables" only refer to variables that are formal parameters? In addition, the text says that the definition of a procedure "binds" its formal parameters. It confuses me that some people say that we "bind" a value to a variable. Obviously this term seems to mean different things when we talk about different types of variables. Can anyone figure out what a bound variable is and what binding means? Finally, unlike a bound variable, what is a free variable? How does all this relate to the area?

+3


source to share


1 answer


There are only two types of variables. Global and lexical. In fact, you can think of the global as a variable root of the lexical scope, in which case there is only one type of variable.

The bind variable is the formal parameters of the current procedure, and everything else, global or associated with previous nested calls, are free variables.

Example:

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

      



Remember, let

ust is a syntactic sutt, so it really is the same:

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

      

The inner lambda is c y

as a bound variable +

and x

are free. In the outer lambda is x

bound and +

free. +

can be global.

+3


source







All Articles