Re: Module Loading in Lisp - Newbie question


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:

var = 20

import a
a.var = 40

import a
import b
print a.var

What will 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 -- despite its name, though, I
don't mean to imply you're an idiot. (Stupid tutorial name,
that.) :)

~ Matthias