📅  最后修改于: 2020-11-03 07:20:51             🧑  作者: Mango
用编程语言的总称来说,一个包旨在提供一种使一组名称彼此分开的方法。在一个程序包中声明的符号不会与在另一个程序包中声明的相同符号冲突。这样,程序包减少了独立代码模块之间的命名冲突。
LISP阅读器会维护一个表,其中包含已找到的所有符号。当找到新的字符序列时,它将创建一个新的符号并将其存储在符号表中。该表称为包。
当前软件包由特殊变量* package *引用。
LISP中有两个预定义的程序包-
common-lisp-它包含所有已定义函数和变量的符号。
common-lisp-user-它使用common-lisp软件包以及所有其他带有编辑和调试工具的软件包;简称为cl-user
下表提供了用于创建,使用和操作包的最常用功能-
Sr.No. | Function and Description |
---|---|
1 |
make-package package-name &key :nicknames :use It creates and returns a new package with the specified package name. |
2 |
in-package package-name &key :nicknames :use Makes the package current. |
3 |
in-package name This macro causes *package* to be set to the package named name, which must be a symbol or string. |
4 |
find-package name It searches for a package. The package with that name or nickname is returned; if no such package exists, find-package returns nil. |
5 |
rename-package package new-name &optional new-nicknames it renames a package. |
6 |
list-all-packages This function returns a list of all packages that currently exist in the Lisp system. |
7 |
delete-package package It deletes a package. |
defpackage函数用于创建用户定义的包。它具有以下语法-
(defpackage :package-name
(:use :common-lisp ...)
(:export :symbol1 :symbol2 ...)
)
哪里,
package-name是软件包的名称。
:use关键字指定此软件包所需的软件包,即定义此软件包中的代码使用的功能的软件包。
:export关键字指定此软件包中外部的符号。
make-package函数还用于创建包。该函数的语法是-
make-package package-name &key :nicknames :use
参数和关键字的含义与以前相同。
创建软件包后,可以将其设为当前软件包来使用此软件包中的代码。程序包内宏使程序包在环境中处于最新状态。
创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。
(make-package :tom)
(make-package :dick)
(make-package :harry)
(in-package tom)
(defun hello ()
(write-line "Hello! This is Tom's Tutorials Point")
)
(hello)
(in-package dick)
(defun hello ()
(write-line "Hello! This is Dick's Tutorials Point")
)
(hello)
(in-package harry)
(defun hello ()
(write-line "Hello! This is Harry's Tutorials Point")
)
(hello)
(in-package tom)
(hello)
(in-package dick)
(hello)
(in-package harry)
(hello)
当您执行代码时,它返回以下结果-
Hello! This is Tom's Tutorials Point
Hello! This is Dick's Tutorials Point
Hello! This is Harry's Tutorials Point
delete-package宏允许您删除程序包。以下示例演示了这一点-
创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。
(make-package :tom)
(make-package :dick)
(make-package :harry)
(in-package tom)
(defun hello ()
(write-line "Hello! This is Tom's Tutorials Point")
)
(in-package dick)
(defun hello ()
(write-line "Hello! This is Dick's Tutorials Point")
)
(in-package harry)
(defun hello ()
(write-line "Hello! This is Harry's Tutorials Point")
)
(in-package tom)
(hello)
(in-package dick)
(hello)
(in-package harry)
(hello)
(delete-package tom)
(in-package tom)
(hello)
当您执行代码时,它返回以下结果-
Hello! This is Tom's Tutorials Point
Hello! This is Dick's Tutorials Point
Hello! This is Harry's Tutorials Point
*** - EVAL: variable TOM has no value