[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Free variables in a LAMBDA which names a function
Date: Thu 26 Mar 87 00:01-EST
From: "Stephen E. Robbins" <STEVER%OZ.AI.MIT.EDU@XX.LCS.MIT.EDU>
Section 5.2 of CLtL point out that there's a distinction between
(LAMBDA ...) as a way of \naming/ a function, and as a function
object.
What are the semantics of a free variable within a LAMBDA which
merely \names/ a function? For example:
(defun foo (arg)
((lambda (x) (+ x arg)) 5))
I would expect:
(setf arg 16)
(foo 3) ==> 8
I'm using a beta test of a Common Lisp which returns 21 as the
result of that (foo 3). Looking through CLtL, I see nothing that
implies that (lambda ...) in the functional position of a form
has its free variables interpreted lexically, rather than dynamically.
8 is the correct result. Function parameters have lexical scope, so the
scope of the "arg" parameter is the entire body of the function. There
is nothing in CLtL that says that lexical scope "skips over" embedded
lambda expressions. My guess is that the compiler you are testing
translated the definition into:
(defun foo (arg)
(#:gensym 5))
(defun #:gensym (x)
(+ x arg))
This would be OK in a dynamically-scoped Lisp (in fact, I believe the
Maclisp compiler did exactly this), but not in Common Lisp. Your
definition should be equivalent to
(defun foo (arg)
(flet ((#:gensyn (x) (+ x arg)))
(#:gensym 5)))
You should also get the result 8 in a dynamically-scoped Lisp, or in
Common Lisp if arg is proclaimed special.
barmar