【发布时间】:2011-07-02 21:15:24
【问题描述】:
我是 CL 的新手,我想学习如何阅读文档字符串并从 REPL 获取其他帮助信息。类似于 Python 中的 help(symbol),或 iPython 中的 symbol?,或 Haskell 的 GHCi 中的 :t 和 :i。
所以,给定一个符号名称,我想知道:
- 绑定什么样的值,如果有的话(函数、变量,根本没有)
- 如果是函数或宏,那么它的位置参数是什么
- 如果有文档字符串,显示它
- 它来自什么包或文件或定义的时间
我发现有(documentation '_symbol_ '_type_),但这并不是我所需要的。我需要知道符号绑定到的值的类型('function、'variable、'compiler-macro 等),然后才能使用documentation。然后它只返回文档字符串,它可能丢失或不足以使用符号。
例如,在 Lisp 中,mapcar 的帮助不是很有用(CLisp 的 REPL):
> (documentation 'mapcar 'function)
NIL
我希望能够看到这样的东西:
>>> map?
Type: builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form: <built-in function map>
Namespace: Python builtin
Docstring:
map(function, sequence[, sequence, ...]) -> list
Return a list of the results of applying the function to the items of
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
sequences have the same length. If the function is None, return a list of
the items of the sequence (or a list of tuples if more than one sequence).
【问题讨论】:
-
你用的是什么编译器?在我的 CL 中,
(documentation 'mapcar 'function)给出:“将 FUNCTION 应用于 LIST 的连续元素。返回 FUNCTION 返回值的列表。” -
(inspect 'mapcar)和(describe 'mapcar)可能是您正在寻找的,尽管这些函数的实际效用在很大程度上取决于特定的实现。 -
公平地说,all 函数的实用性很大程度上取决于实现。 :-)
-
谢谢你,@Daniel。确实,
inspect和describe正是我想要的。谢谢你。如果您将其写为答案,我可以接受。 @Ken:我同时安装了 CLisp 和 SBCL,但直到现在我主要使用 CLisp(它支持 readline 的 REPL 对我来说似乎更好)。 SBCL 的 docstring 确实有用得多,但它仍然只是一个 docstring(仍然需要以某种方式找到符号类型和参数的列表)。 -
好的,接受@Rainer 的回答。
标签: documentation lisp common-lisp read-eval-print-loop