如何在 LISP (sbcl) 中永久保存宏
假设我定义了一个宏
(defmacro foo(x)
(print x))
现在我希望能够在将来始终在我的所有 lisp 文件中加载这个宏 我应该在哪里保存这个宏?
Lets say i define a macro
(defmacro foo(x)
(print x))
Now i want to be able to always load this macro in all my lisp files in future where should i save this macro?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个 CL 实现都有一个在启动时调用的 init 文件。对于 SBCL 来说是 ~/.sbclrc。如果您安装了quicklisp,通常quicklisp 中也会有一些设置代码。
因此,您可以添加一行:
每次启动 SBCL 时都会加载它。
Every CL implementation has an init file it calls on start. For SBCL that is ~/.sbclrc. Usually also quicklisp has some setup code in there if you installed quicklisp.
So you could add a line:
And it'll get loaded each time you start SBCL.
你想拥有一个图书馆。
库可以包含任意数量的定义,而不仅仅是宏。
定义系统(这是库、框架和应用程序 - 软件单元的更通用术语)的事实上的标准方法是使用 ASDF。
假设您将宏放入一个 lisp 文件中:
然后将其放在 ASDF 已知的目录下。一个有用的目录是
~/common-lisp/
,所以让我们使用~/common-lisp/my-util/
。在同一目录中,放置系统定义文件:现在您可以将此实用程序加载到任何 lisp 交互中,例如在 repl 上:
或者在不同的系统中:
在实用程序库的情况下,您经常想要
使用 他们的包,这样你就不需要对符号进行包限定。
如果您想要只在您的计算机上运行的东西(例如 REPL 使用的快捷方式或一次性脚本),有时可以通过将东西添加到实现的 init 文件中来摆脱困境。
You want to have a library.
A library can contain any number of definitions, not only macros.
The de facto standard way to define systems (which is a more general term for libraries, frameworks, and applications — units of software) is to use ASDF.
Let's say that you put your macro into a lisp file:
Then you put that under a directory that is known to ASDF. One useful directory for that is
~/common-lisp/
, so let's use~/common-lisp/my-util/
. In the same directory, put the system definition file:Now you can load this utility into any lisp interaction, e. g. on the repl:
Or in a different system:
In the case of utility libraries, you often want to
use
their package so that you don't need to package-qualify the symbols.If you want things that only work on your computer (like shortcuts for your REPL use or one-off scripts), you can sometimes get away with adding things to your implementation's init file.