Page 1 of 1

scope problem

Posted: Sat Sep 18, 2010 10:35 am
by hiraditya
Hello.
I'm facing a strange scope problem: simply put...

(defun bar(expr x v)
(setf result expr)
(setf x v)
(eval result)
)

> (bar '(* x 2) 'x 10)
Error in KERNEL::UNBOUND-SYMBOL-ERROR-HANDLER: the variable X is unbound.
[Condition of type UNBOUND-VARIABLE]
-------------------------------------------------------------------------------------------------
but when i type the same commands on system the result comes...but actually i want to implement this in a function for my code

Re: scope problem

Posted: Sat Sep 18, 2010 12:16 pm
by gugamilare
You are obviously trying to do something the wrong way, you shouldn't need to use EVAL and neither to assign symbol's values (like you are trying to do with X). Besides, you didn't define the variable RESULT, that way the code won't do what you expect it to.

By doing

Code: Select all

(setf x v)
You are not changing the global value of X, but just the value of X as a local variable inside the function BAR. EVAL evaluates its expression in a clean environment, that it, without ANY local variables, including the variable X of BAR.

In any case, you have to change the approach of solving your problem, whatever that is. I suggest that you get some book about Common Lisp and learn it before anything else, I recommend the book Practical Common Lisp.

Re: scope problem

Posted: Sat Sep 18, 2010 12:32 pm
by hiraditya
yep...
i got the result

i defined the variables on the top
by using defparameter
(defparameter x 1)
etc...

then i got the result.

btw thanks for the book.