Hello,
while messing a bit with closures I encountered strange behavior.
I just wrote a closure which holds an one-dimensional array of
length n. The closures "method" 'set takes an array index and
sets the corresponding element to (<index> 0) when called for the
first time on <index>. Subsequent calls increase the second value
of the tuple by 1.
Under GCL and ACL the code works as I, with my
very primitive knowledge of Lisp, expect.
Anyway, here's the code:
(defun foo (n)
(let ((arr (make-array n :initial-element nil)))
(labels ((this (cmd &rest args)
(case cmd
(set
(let ((i (first args)))
(if (eq (aref arr i) nil)
(setf (aref arr i) `(,i 0))
(incf (second (aref arr i))))))
(get arr))))
#'this)))
(defun main ()
(let ((board (foo 4)))
(funcall board 'set 0)
(funcall board 'set 1)
(funcall board 'set 2)
(funcall board 'set 2)
(funcall board 'set 3)
(funcall board 'set 3)
(funcall board 'get)))
This is what clisp, cmucl and sbcl return:
#((0 2) (1 2) (2 2) (3 2))
This is what gcl and acl return:
#((0 0) (1 0) (2 1) (3 1))
I hope you can tell me, what's going on here.
Regards, Christian
Oh, btw don't wonder about the (labels ((this ...))) construction.
I reduced the closure to its bare essentials. When it was more complex
it needed to call itself. I don't know, if named local functions like
that are common common lisp style.