global variable

Discussion of Common Lisp

global variable

Postby mparsa » Fri Jul 13, 2012 1:05 am

Can we define global variable in lisp !? If we have it in lisp, how we can define it ?
Posts: 26
Joined: Mon Jun 25, 2012 6:15 am

Re: global variable

Postby wvxvw » Fri Jul 13, 2012 2:46 am

Generally, all variables are defined on package level; if that's global enough for you, then yes (they may be shared between different functions). ... 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) for example.
Posts: 127
Joined: Sat Mar 26, 2011 6:23 am

Re: global variable

Postby garethw » Fri Jul 13, 2012 1:07 pm

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" (, 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!
User avatar
Posts: 43
Joined: Fri Jul 13, 2012 12:56 pm
Location: Ottawa, ON

Re: global variable

Postby virex » Tue Jul 31, 2012 5:15 am

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)

Creates a global variable and assings it the value 3.

Code: Select all
(defvar foo 3 "This is foo, which should be 3")

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.

If you want to get rid of your variable in some way, you can
Code: Select all
(unintern 'foo)

If you've defined foo in one package, you need to use some special syntax to get it from another package:

Code: Select all
(in-package :CL-USER)
(defvar foo 3)
(defpackage (:MY-PACK
  (:use :CL))
(in-package :MY-PACK)
(eql cl-user:foo 3)

Should return T

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)

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.
Posts: 17
Joined: Fri Oct 28, 2011 3:41 pm

Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 7 guests