28.02.2014 Views

Burke_et_al-NIL_Reference_Manual_0286-1984

Burke_et_al-NIL_Reference_Manual_0286-1984

Burke_et_al-NIL_Reference_Manual_0286-1984

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Scope. Extent. and Hinding 12 Nil. Manu411<br />

distinguished from more "ordinary'· lexic<strong>al</strong>ly scoped variablcs.<br />

variable speci<strong>al</strong> is with defvar (p41ge 24). ror instance.<br />

(defvar -leaves-)<br />

(defun find-<strong>al</strong>I-leaves (tree)<br />

(l<strong>et</strong> (-leaves- nil»<br />

(find-<strong>al</strong>l-leaves-l tree)<br />

-leav.8s-<br />

) )<br />

(defun find-<strong>al</strong>l-leaves-l (tree)<br />

(cond «atom tree)<br />

; Empty sctofleaves<br />

; o rO\lcl over the tree<br />

; And r<strong>et</strong>urn the leaves found<br />

One way to glob<strong>al</strong>ly declare a<br />

(cond «not (memq tree -leaves-»<br />

(s<strong>et</strong>q -leaves- (cons tree -leaves-»»)<br />

(t (find-<strong>al</strong>l-1eaves-! (car tree»<br />

(find-<strong>al</strong>l-leaves-1 (cdr tree»»)<br />

There are more esoteric (or SCIIEMIHike) ways in which the above could have been perfunned.<br />

without the. usc of the speci<strong>al</strong> variable -Ieaves-. but the above is fairly straightforward. fairly<br />

effident. ,md will <strong>al</strong>so run (both k terpreled and -compiled) compatibly in MACUSP and !.lSI'<br />

MACHINE I.ISP.<br />

The above intuitive (or. if you prefer. hand-waving and vague) description can now be used<br />

to more fonn<strong>al</strong>ly define the tenns of scope and extellt which are used to describe the accessibility<br />

and lif<strong>et</strong>imes of things. of which variable bindings are one instance. The scope of som<strong>et</strong>hing tells<br />

where it may be v<strong>al</strong>idly referred to~ To say that som<strong>et</strong>hing has lexic<strong>al</strong> scope then means that it<br />

iHay UC U)CU cJllywiaclc ·'i.cxluaiiy'" within Lhc conSlfUct which "creatcs" the objcct (e.g.. the<br />

lambda-expression which binds a variable). Note that this docs not in itself imply that the<br />

reference becomes inv<strong>al</strong>id if that construct is exited. That dimension is the extelll of the object.<br />

which tens the lime during which the object (e.g.. variable binding) is v<strong>al</strong>id dynamic extent<br />

means that the object (reference) is only vaHd during the execution of the construct. illdefillite<br />

extellt means that there is no such limitation. Variable bindings in the <strong>NIL</strong> interpr<strong>et</strong>er (which are<br />

not speci<strong>al</strong>) have lexic<strong>al</strong> scope and indefinite· extent lbismeans upward funarg capability.<br />

indefinite scope means that there is no restriction on wbere a v<strong>al</strong>id reference may occur from.<br />

This is the case with speci<strong>al</strong> variables; the "free" references may be made from any piece of<br />

code. The bindings of such variables. however, have only dynamic extent; they become inv<strong>al</strong>id<br />

(are "unbound") when the binding construct is exited. This combination of scope and extent,<br />

which is quite common, is· referred to as dYllamic scope.<br />

Now, for the pragmatics. The current <strong>NIL</strong> compiler actu<strong>al</strong>ly only implements loc<strong>al</strong> scope<br />

instead of lexic<strong>al</strong> scope. Its capabilities lie only in dctennining when it is losing. In many cases,<br />

this· docs nOl matter because the constructs being used are expanded out into other construc~<br />

making the references loc<strong>al</strong>. This is what happens for mapcar, for instance: in the construct<br />

(l<strong>et</strong> «22 (computate»)<br />

(mapc #'(lambda (x) (mumblify x 22» some-list})<br />

the reference to zz within the lambda expression is a non-loc<strong>al</strong> (but lexic<strong>al</strong>) reference. That<br />

expression is recoded by the compiJer~ however. as an iteration without a separate function. in<br />

which the reference become loc<strong>al</strong>.<br />

MC:<strong>NIL</strong>MAN:BIND 27<br />

23-DEC-83

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!