[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Lexical closures (bug?)
Date: Mon, 15 Aug 88 14:58:32 pdt
From: lbaum@BOEING.COM (Larry Baum)
We were working thru the exercises in Charniak, Riesbeck & McDermot,
Artificial Intelligence Programming, and tried the following:
(defun compose (&rest functions)
#'(lambda (x)
(do ((fns functions (cdr fns))
(value x (funcall (car fns) value)))
((null fns) value))))
However, when I try this on my 36xx, with either Genera 7.1 or 7.2, the
control stack overflows.
On the 36xx machines, &REST arguments to a function are CONSed on the stack.
They go away when the function returns. When COMPOSE returns, FUNCTIONS, which
the closure uses, now looks into the stack somewhere.
Yes, this is a violation of the Common Lisp spec. &REST lists are supposed to
be freshly consed, with indefinite extent.
You can work around the problem by consing up a "real" list of functions:
(defun compose (&rest functions)
#+LISPM (setf functions (copy-list functions))
#'(lambda (x)
(do ((fns functions (cdr fns))
(value x (funcall (car fns) value)))
((null fns) value))))
-- Stephen