From: Li-San Wang
Subject: Having trouble with global variable
Date: 
Message-ID: <6vc062$59h$1@geraldo.cc.utexas.edu>
I am new to Lisp.  Here is a problem I have todaywith global variable
*open*:

(defvar *open*)

(defun ENQUEUE  (state prev S)
    (if (null S) (setf S (acons state prev nil))
 (setf (cdr (last S)) (acons state prev nil))
    )
)

(defun DEQUEUE  (state prev S) (progn
    (setf state (GETSTATE S))
    (setf prev  (GETPREV  S))
    (setf S (cdr S)) ))

(defun make-queue () (cons nil nil))

(defun BFS (S depth)
    (let (this prev ch ch_list (count 0))
         (ENQUEUE S nil *open*)                                           ;
put S into *open*
              (loop while (not (EMPTYQUEUE *open*)) do         ; while
*open* noempty
......

ENQUEUE seems not to change *open* at all!  I think it is some problem
with scope.  But I don't know how to solve it!  Can anyone tell me where I
go wrong?
Thanks!

(It seems to work if I just copy the code of ENQUEUE to BFS.  But I still
want
to work this out about function calls...)
From: Barry Margolin
Subject: Re: Having trouble with global variable
Date: 
Message-ID: <birS1.36$8A3.788625@burlma1-snr1.gtei.net>
In article <············@geraldo.cc.utexas.edu>,
Li-San Wang <······@mail.utexas.edu> wrote:
>ENQUEUE seems not to change *open* at all!  I think it is some problem
>with scope.  But I don't know how to solve it!  Can anyone tell me where I
>go wrong?
>Thanks!

Functions are called by value in Lisp.  Setting S in ENQUEUE only changes
the value of S, not the variable that was used in the calling expression.

You may want to change your functions to macros:

(defmacro enqueue (state prev s)
  `(setf ,s (append ,s (a cons ,state ,prev nil))))

(defmacro dequeue (state prev s)
  (let ((var (gensym)))
    `(let ((,var ,s)) ;; prevent repeated evaluation
       (setf ,state (getstate ,var)
             ,prev (getprev ,var)
             ,s (cdr ,var)))))

Probably a better solution would be to use DEFSTRUCT to represent states,
and then side effect the structure slots.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.