scope problem

Discussion of Common Lisp
Post Reply
hiraditya
Posts: 2
Joined: Sat Sep 18, 2010 10:25 am

scope problem

Post by hiraditya » Sat Sep 18, 2010 10:35 am

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

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: scope problem

Post by gugamilare » Sat Sep 18, 2010 12:16 pm

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.

hiraditya
Posts: 2
Joined: Sat Sep 18, 2010 10:25 am

Re: scope problem

Post by hiraditya » Sat Sep 18, 2010 12:32 pm

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.

Post Reply