Thursday, October 8, 2009

LISP Package

using namespace std to use the symbol in the STL . In the C#, we use the Using System.Net to use the symbols in different name space. If you think that Name conflict is not a big problem, you are wrong. Suppose, for instance, you’re writing a program and decide to use a third-party library. You don’t want to have to know the name of every function, variable, class, or macro used in the internals of that library in order to avoid conflicts between those names and the names you use in your program. You’d like for most of the names in the library and the names in your program to be considered distinct even if they happen to have the same textual representation. At the same time, you’d like certain names defined in the library to be readily accessible—the names that make up its public API, which you’ll want to use in your program.In Common Lisp, this namespace problem boils down to a question of controlling how the reader translates textual names into symbols: if you want two occurrences of the same name tobe considered the same by the evaluator, you need to make sure the reader uses the samesymbol to represent each name. Conversely, if you want two names to be considered distinct,even if they happen to have the same textual name, you need the reader to create differentsymbols to represent each name.

(Examples and some text from book Practical Common Lisp)

If you have experiment on C++ or C# language , you must be familiar with the name space which is used to resolve the name conflict in a large project. In C++, we use the





To read code in one package, you need to make it the current package with the IN-PACKAGE macro.


(in-package :yourpackage)


If you type this in the REPL. it will change the value of the *package*, affecting how the REPL reads subsequent expressions.


Packaging Reusable Libraries


If you are using the COMMON-LISP package, because you’ll need access to standard functions within COM.GIGAMONKEYS.TEXT-DB. The :export clause specifies names that will be external in COM.GIGAMONKEYS.TEXT-DB and thus accessible in packages that :use it.



(defpackage :com.gigamonkeys.email-db


    (:use :common-lisp :com.gigamonkeys.text-db))


If you only want to use a function in other package, but others names have the conflict with the current package you are using. You can only import the specify function from other package.



(defpackage :com.gigamonkeys.email-db


    (:use :common-lisp :com.gigamonkeys.text-db)


    (:import-from :com.acme.email :parse-email-address))


Occasionally you’ll run into the opposite situation—a package may export a bunch of names you want to use and a few you don’t. Rather than listing all the symbols you do want to use in an :import-from clause, you can instead :use the package and then list the names you don’t want to inherit in a :shadow clause.


You can use the :nicknames to give the package a nickname, like the Common-Lisp whose nickname is CL-USER. If you want to give some documentation to the package, you can use the :documentation.

No comments:

Post a Comment