Hi all,
I started playing around with Lisp for the past couple of days. I am a
python programmer so please be gentle. :-)
As I understand, there are many ways to do the the python equivalent
of an import in Lisp. You can either to a (load :filename) or
(require :filename). There is also the (import ...) thingy for which I
appear to need a (defpackage ... ) and (in-package ...). Given my
Python background, I was having a bit of a disconnect understanding
the concept in Lisp. When we do a import modulename in python, and if
it has already been imported, python does nothing. That way if there
are global variables present in the module imported, even if I import
them several times again in other modules, it would not get imported
again and my module variables will remain intact. Is there anyway, in
which, I could do the same with Lisp?
Say, I have a module called lib.lisp which has the single line.
(setf *var* 20)
I then have lib1.lisp, which has the following.
(require :lib)
(setf *var* 40)
I then have lib2.lisp which requires both lib and lib1.
(require :lib1)
(require :lib)
Then the value of *var* when finally executing lib2 is 20 and not 40.
I suspect that I am not following the lisp style of programming. If
so, can you please point me in the right direction?
Regards,
Vagmi
Hi,
> Say, I have a module called lib.lisp which has the single line.
>
> (setf *var* 20)
>
> I then have lib1.lisp, which has the following.
>
> (require :lib)
> (setf *var* 40)
>
> I then have lib2.lisp which requires both lib and lib1.
>
> (require :lib1)
> (require :lib)
>
> Then the value of *var* when finally executing lib2 is 20 and not 40.
In Python, if I have three modules, like this:
# a.py
var = 20
# b.py
import a
a.var = 40
# c.py
import a
import b
print a.var
What will c.py print?
Now, I think what confuses is you is the fact that (a) Python modules
are namespaces, whereas Lisp files aren't, and (b) you don't import
definitions in Lisp as you do in Python, but rather symbols (names).
If you want to define namespaces (packages, as they are called in
Common Lisp), you have to DEFPACKAGE them explicitely and then use IN-
PACKAGE to define stuff within them.
By the way, you don't normally use SETF to declare variables. If you
want to declare a special variable, use DEFVAR or DEFPARAMETER.
Lexical variables are introduced by LET (or LAMBDA, or DEFUN, or...).
There's a very nice introduction to the Common Lisp package system at
http://www.flownet.com/gat/packages.pdf -- despite its name, though, I
don't mean to imply you're an idiot. (Stupid tutorial name,
that.) :)
~ Matthias
Hi again,
> # a.py
> var = 20
>
> # b.py
> import a
> a.var = 40
>
> # c.py
> import a
> import b
> print a.var
>
> What will c.py print?
Whoops. I guess I misunderstood your question entirely. Umm... I'm
sorry. Reading that package tutorial is still a good idea, though. ;)
(And yes, do take a look at ASDF. That might actually answer your
question.)
~ Matthias
Vagmi wrote:
> As I understand, there are many ways to do the the python equivalent
> of an import in Lisp. You can either to a (load :filename) or
> (require :filename). There is also the (import ...) thingy for which I
> appear to need a (defpackage ... ) and (in-package ...). Given my
You're confusing two different things. Possibly three different things.
Four things. _Amongst_ the things are
1) there is no concept of "module" in common lisp
2) there are files, which are more or less what you'd expect. Load them
with LOAD
3) and there are packages, which are usually used to define public
interfaces between different bodies of code (e.g. yours vs Joes, or
application vs library it depends on), but they specify visibility of
_symbols_ and have no close relationship with files. This is what you
get with DEFPACKAGE and friends.
4) there are "de facto" standard "defsystem" libraries which allow you
to specify that a collection of files - e.g. that make up an application
or library - is a system that can be compiled/loaded together, and name
the dependencies between them. ASDF and MK-DEFSYSTEM are portable (or
at least, widely ported) and widely used, and there may be others
available/bundled with whatever Lisp you're using.
5) REQUIRE is deprecated, according to the standard. Your Lisp
implementation might have made it do something useful (like, run LOAD,
or run ASDF), or they might not, but who knows what?
6) an almost fanatical devotion to the Pope
> Say, I have a module called lib.lisp which has the single line.
>
> (setf *var* 20)
Incidentally, I think you really wanted to use DEFPARAMETER or DEFVAR
here (see the Hyperspec for the difference between them): SETF of a
previously undefined variable may do Bad Things.
No being a python programmer I'm only really guessing at what problem
you actually want to solve here, but I believe you're looking for a way
to split your code between multiple files (thing #4 above) and should
really be looking at ASDF or something like that. Having said that, my
second guess is that if you're new to Lisp, all you really need to know
is that this stuff _exists_ and is waiting for the first time you write
a program big enough to need it: in the meantime you can get on with
learning the language and don't have to take time out to implement it
yourself.
The Hyperspec's at
http://www.lispworks.com/documentation/HyperSpec/Front/index.htm
For ASDF information, see http://www.cliki.net/asdf
-dan
Vagmi <··············@gmail.com> writes:
> Hi all,
>
> I started playing around with Lisp for the past couple of days. I am a
> python programmer so please be gentle. :-)
Welcome. But no promises :-)
> As I understand, there are many ways to do the the python equivalent
> of an import in Lisp. You can either to a (load :filename) or
> (require :filename). There is also the (import ...) thingy for which I
> appear to need a (defpackage ... ) and (in-package ...). Given my
> Python background, I was having a bit of a disconnect understanding
> the concept in Lisp. When we do a import modulename in python, and if
> it has already been imported, python does nothing. That way if there
> are global variables present in the module imported, even if I import
> them several times again in other modules, it would not get imported
> again and my module variables will remain intact. Is there anyway, in
> which, I could do the same with Lisp?
>
> Say, I have a module called lib.lisp which has the single line.
>
> (setf *var* 20)
>
> I then have lib1.lisp, which has the following.
>
> (require :lib)
> (setf *var* 40)
>
> I then have lib2.lisp which requires both lib and lib1.
>
> (require :lib1)
> (require :lib)
>
> Then the value of *var* when finally executing lib2 is 20 and not 40.
> I suspect that I am not following the lisp style of programming. If
> so, can you please point me in the right direction?
If you use defvar rather than setf or setq, then what you want will be
the case, and *var* will remain 20 (assuming it had not already been
set in a different module). The tie of variables to modules is very
loose, so you can either associate a variable with a module or not.
Generally, we tend to use the rule of thumb that an unexported name is
intended to remain internal and not for user use, so if you give lib a
package of its own and don't export *var*, chances are its usage will
not conflict with a variable named *var* in another module (which
presumably has its own package).
--
Duane Rettig ·····@franz.com Franz Inc. http://www.franz.com/
555 12th St., Suite 1450 http://www.555citycenter.com/
Oakland, Ca. 94607 Phone: (510) 452-2000; Fax: (510) 452-0182