How to see docstrings and other symbol information

2019-03-14 09:49发布

问题:

I'm completely new to CL, and I'd like to learn how to read documentation strings and get other help information from the REPL. Something like help(symbol) in Python, or symbol? in iPython, or :t and :i in Haskell's GHCi.

So, given a symbol name, I'd like to be able to know:

  • what kind of value it is bound to, if any (a function, a variable, none at all)
  • if it is a function or a macro, then what are its positional arguments
  • if it has a docstring, show it
  • what package or file it is coming from or when it was defined

I found there is (documentation '_symbol_ '_type_), but it is not exactly what I need. I need to know the type of value the symbol is bound to ('function, 'variable, 'compiler-macro, etc.) before I can use documentation. Then it returns only the docstring, it may be missing or not sufficient to use the symbol.

For example, in Lisp, the help for mapcar is not very useful (CLisp's REPL):

> (documentation 'mapcar 'function)
NIL

I'd like to be able to see something like this instead:

>>> 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).

回答1:

As mentioned Common Lisp has standard functions: DESCRIBE, INSPECT and DOCUMENTATION. Typical Lisp IDEs also have these bound to keys and menus.

For standard Common Lisp functionality most IDEs directly link to the Common Lisp HyperSpec documentation with a keystroke.

Most IDEs also have keystrokes to show the arglist and the documentation. There is also the 'arglist on space' functionality.

LispWorks specific examples: LispWorks Argument list information and LispWorks Expressions menu

I can recommend to read the IDE manual for Slime, LispWorks Editor, Allegro CL's ELI, or whatever IDE you are using.



回答2:

Regarding your question about getting the type of symbol: there is no such thing. Or, more precisely, symbols are not just names of other objects, but themselves objects of the type SYMBOL. Each symbol can have both a variable value and a function value. To check if it has a variable value, use BOUNDP, and to check for a function value FBOUNDP.