Hi,
Can we define global variable in lisp !? If we have it in lisp, how we can define it ?
global variable
Re: global variable
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.
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.
Re: global variable
Hi mparsa,mparsa wrote:Hi,
Can we define global variable in lisp !? If we have it in lisp, how we can define it ?
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!
~garethw
Re: global variable
mparsa wrote:Hi,
Can we define global variable in lisp !? If we have it in lisp, how we can define it ?
Code: Select all
(defvar foo 3)
Code: Select all
(defvar foo 3 "This is foo, which should be 3")
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.
If you want to get rid of your variable in some way, you can
Code: Select all
(unintern 'foo)
Code: Select all
(in-package :CL-USER)
(defvar foo 3)
(defpackage (:MY-PACK
(:use :CL))
(in-package :MY-PACK)
(eql cl-user:foo 3)
Many packages only export a limited number of symbols. If the variable you need is not exported, you can retrieve it in the following way:
Code: Select all
(defpackage :protected-test
(:use :CL :CL-USER)
(:export :baz)
(in-package :protected-test)
(defvar baz 3)
(defvar foo 4)
(in-package :CL-USER)
(eql protected-test:baz 3)
(eql protected-test:foo 4)
(eql protected-test::foo 4)