有没有办法找出原始函数(内置)在SBCL中是如何准确定义的?
我正在使用 Emacs、SBCL 和 Slime 学习 Common Lisp。
我想确切地知道内置函数的代码定义是什么。
我知道如何使用(documentation ...)和(describe ...)。但是,它们仅提供高级别的信息。我想看看代码细节。
以nth内置函数为例。
Documentation 给我们:
CL-USER> (documentation 'nth 'function)
"Return the nth object in a list where the car is the zero-th element."
Describe 给我:
CL-USER> (describe 'nth)
COMMON-LISP:NTH
[symbol]
NTH names a compiled function:
Lambda-list: (SB-IMPL::N LIST)
Declared type: (FUNCTION (UNSIGNED-BYTE LIST) (VALUES T &OPTIONAL))
Derived type: (FUNCTION (T T) (VALUES T &OPTIONAL))
Documentation:
Return the nth object in a list where the car is the zero-th element.
Inline proclamation: MAYBE-INLINE (inline expansion available)
Known attributes: foldable, flushable, unsafely-flushable
Source file: SYS:SRC;CODE;LIST.LISP
(SETF NTH) names a compiled function:
Lambda-list: (SB-KERNEL::NEWVAL SB-IMPL::N LIST)
Derived type: (FUNCTION (T UNSIGNED-BYTE LIST) (VALUES T &OPTIONAL))
Inline proclamation: INLINE (inline expansion available)
Source file: SYS:SRC;CODE;SETF-FUNS.LISP
(SETF NTH) has a complex setf-expansion:
Lambda-list: (SB-IMPL::N LIST)
(undocumented)
Source file: SYS:SRC;CODE;DEFSETFS.LISP
; No value
我想看到类似的东西:
(unknown-command 'nth)
这将返回如下内容:
(defun nth (x xs)
(if (equal x 0)
(car xs)
(my-nth (- x 1) (cdr xs))))
Lisp 语言非常棒,拥有由优秀程序员构建的庞大生态系统。我希望有一些工具或命令。
谢谢
回答
首先,一些一般性说明
- 在你自己的代码中,点击Meta-.应该带你到代码的源头
- 对于通过 Quicklisp 安装的库,这也将“正常工作”。
现在对于 SBCL 代码本身:
-
如果代码是在“预期的地方”,击中Meta-.的内置函数(如
nth在你上面的例子)将还带你到它的来源。我相信默认值是/usr/share/sbcl-source/src/code/但可能有一种配置它的方法。 -
但是,还有另一种实用的方式来查看:如果您查看
(describe ...)上面的输出,该行是:
Source file: SYS:SRC;CODE;LIST.LISP
Source file: SYS:SRC;CODE;LIST.LISP
-
注意:不是最后一行,就是 for
(setf nth),稍微有点不同 -
这会告诉您可以期望在 SBCL 源代码中的哪个文件中找到函数定义。
-
因此,在 [repo](https://github.com/sbcl/sbcl/tree/master/src) 中,如果您找到
src/code/list.lisp,您应该找到您正在寻找的定义;复制在这里:
- `SB-EXT:SET-SBCL-SOURCE-LOCATION` can be used in `~/.sbclrc` to set where the sources are.