Ped*_*ino 1 sbcl common-lisp slime built-in code-documentation
我正在使用 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."
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
我想看到类似的东西:
(unknown-command 'nth)
Run Code Online (Sandbox Code Playgroud)
这将返回如下内容:
(defun nth (x xs)
(if (equal x 0)
(car xs)
(my-nth (- x 1) (cdr xs))))
Run Code Online (Sandbox Code Playgroud)
Lisp 语言非常棒,拥有由优秀程序员构建的庞大生态系统。我希望有一些工具或命令。
谢谢
如果代码是在“预期的地方”,击中Meta-.的内置函数(如nth在你上面的例子)将还带你到它的来源。我相信默认值是/usr/share/sbcl-source/src/code/但可能有一种配置它的方法。
但是,还有另一种实用的方式来查看:如果您查看(describe ...)上面的输出,该行是:
Source file: SYS:SRC;CODE;LIST.LISP
Run Code Online (Sandbox Code Playgroud)
注意:不是最后一行,就是 for (setf nth),稍微有点不同
这会告诉您可以期望在 SBCL 源代码中的哪个文件中找到函数定义。
因此,在 [repo](https://github.com/sbcl/sbcl/tree/master/src) 中,如果您找到src/code/list.lisp,您应该找到您正在寻找的定义;复制在这里:
Source file: SYS:SRC;CODE;LIST.LISP
Run Code Online (Sandbox Code Playgroud)