[2-14] How do I find the argument list of a function? How do I get the function name from a function object?

There is no standard way to find the argument list of a function,
since implementations are not required to save this information.
However, many implementations do remember argument information, and
usually have a function that returns the lambda list. Here are the
commands from some Lisp implementations:

   Lucid:                               arglist
   Allegro:                             excl::arglist
   Symbolics:                           arglist
   LispWorks:                           lw:function-lambda-list

CMU Common Lisp, new compiler:
   #+(and :CMU :new-compiler)
   (defun arglist (name)
     (let* ((function (symbol-function name))
            (stype (system:%primitive get-vector-subtype function)))
       (when (eql stype system:%function-entry-subtype)
         (cadr (system:%primitive header-ref function

The draft ANSI standard does include FUNCTION-LAMBDA-EXPRESSION and
FUNCTION-KEYWORDS, which can be used to create an ARGLIST function.

If you're interested in the number of required arguments you could use

   (defun required-arguments (name)
     (or (position-if #'(lambda (x) (member x lambda-list-keywords))
                      (arglist name))
         (length (arglist name))))

To extract the function name from the function object, as in
        (function-name #'car) ==> 'car
use the following vendor-dependent functions:

   Symbolics: (si::compiled-function-name <fn>)
	(unless (si:lexical-closure-p <fn>) ...)
   Lucid:     (sys::procedure-ref <fn> SYS:PROCEDURE-SYMBOL)
	(when (sys:procedurep <fn>) ..)
   Allegro:   (xref::object-to-function-name <fn>)
   CMU CL:    (kernel:%function-header-name <fn>)
   AKCL:      (system::compiled-function-name <fn>)
   MCL:       (ccl::function-name <fn>)
   LispWorks: (system::function-name <fn>)

If a vendor-dependent function does not exist, the following
(inefficient) code maps over all symbols looking for one whose
function-cell matches the function object.

(defun function-name (fobject)
   (do-all-symbols (fsymbol)
      (when (and (fboundp fsymbol)
                 (eq (symbol-function fsymbol) fobject))
        (return fsymbol))))

If a vendor supports FUNCTION-LAMBDA-EXPRESSION, the third value is
the name of the function, if available. 
Go Back Up

Go To Previous

Go To Next