Generally, all variables are defined on package level; if that's global enough for you, then yes (they may be shared between different functions).http://www.lispworks.com/documentation/ ... defpar.htm here's more info about it, but it is more common to call these dynamic variables, not global. Variables may also be imported from/to package, but as a general case there's no concept of user-defined global variable (i.e. accessible out of the box in every scope). The later would be considered a bad design (if it existed), rather than omission. There are couple of built-in global constants, but they employ the same mechanism as above, (they are defined in common-lisp package, which is the one loaded by default) http://clhs.lisp.se/Body/v_t.htm for example.

mparsa wrote:Hi,Can we define global variable in lisp !? If we have it in lisp, how we can define it ?

Hi mparsa,

I'm a complete newb, but I highly recommend you consult Chapter 6 of Peter Seibel's "Practical Common Lisp" (http://www.gigamonkeys.com/book/variables.html), in which he discusses both lexical and dynamic variables. He draws rough analogies to local and global variables, respectively - but it's important to understand the rather powerful capabilities that dynamic variables give you that a global variable in, say, C, does not.

You should take a look at that PCL book, and go through it slowly as I've been doing, following along with the exercises. There's a lot to absorb there - don't worry about understanding the deeper details at first. Then come up with a small project of your own, and try to implement it... and go back and read the sections more carefully - you might be surprised, as I was, how much better you understand it once you have a practical application of your own to apply it to.

And if, like me, you believe in rewarding people for good work, you should buy a copy of the book like I did!

Does the same, but associates a docstring to it, which will be shown if you (inspect 'foo) or (describe 'foo). Useful if you're working with a lot of files because you don't have to dig the definition of foo up to see if you left any comments there.

After you've created a variable with defvar, it's best to use setter functions (Setf or setq) to change it's value, as some implementations allow changing the value of global variables by calling defvar again, but others don't.

The one-to-last line will give an error, because protected-test does not export foo, so you can't access it like that. The last line does work though, because you're using the double semicolon to indicate that you do want to access the protected variable. It's usually bad form to access variables that are not exported, they're not exported for a reason.