如何在 LISP (sbcl) 中永久保存宏

发布于 2025-01-12 11:54:54 字数 125 浏览 3 评论 0原文

假设我定义了一个宏

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沧桑㈠ 2025-01-19 11:54:54

每个 CL 实现都有一个在启动时调用的 init 文件。对于 SBCL 来说是 ~/.sbclrc。如果您安装了quicklisp,通常quicklisp 中也会有一些设置代码。

因此,您可以添加一行:

(load #P"path/to/your/macro/file.lisp")

每次启动 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:

(load #P"path/to/your/macro/file.lisp")

And it'll get loaded each time you start SBCL.

朦胧时间 2025-01-19 11:54:54

你想拥有一个图书馆。

库可以包含任意数量的定义,而不仅仅是宏。

定义系统(这是库、框架和应用程序 - 软件单元的更通用术语)的事实上的标准方法是使用 ASDF。

假设您将宏放入一个 lisp 文件中:

;;;; my-util.lisp

(in-package cl-user)

(defpackage my-util
  (:use cl))

(in-package my-util)

(defmacro foo (x)
  `(whatever ,x etc))

然后将其放在 ASDF 已知的目录下。一个有用的目录是 ~/common-lisp/,所以让我们使用 ~/common-lisp/my-util/。在同一目录中,放置系统定义文件:

;;;; my-util.asd

(in-package asdf-user)

(defsystem "my-util"
  :components ((:file "my-util")))

现在您可以将此实用程序加载到任何 lisp 交互中,例如在 repl 上:

CL-USER> (asdf:load-system "my-util")

或者在不同的系统中:

;;;; my-application.asd

(in-package asdf-user)

(defsystem "my-application"
  :depends-on ("my-util")
  ...)

在实用程序库的情况下,您经常想要使用 他们的包,这样你就不需要对符号进行包限定。

如果您想要只在您的计算机上运行的东西(例如 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:

;;;; my-util.lisp

(in-package cl-user)

(defpackage my-util
  (:use cl))

(in-package my-util)

(defmacro foo (x)
  `(whatever ,x etc))

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:

;;;; my-util.asd

(in-package asdf-user)

(defsystem "my-util"
  :components ((:file "my-util")))

Now you can load this utility into any lisp interaction, e. g. on the repl:

CL-USER> (asdf:load-system "my-util")

Or in a different system:

;;;; my-application.asd

(in-package asdf-user)

(defsystem "my-application"
  :depends-on ("my-util")
  ...)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文