Per le funzioni interpretate si dovrebbe essere in grado di utilizzare function-lambda-expression
.
Per le funzioni compilate, ahimè, questa funzione spesso restituisce nil
, quindi si dovrà utilizzare una funzione implementazione-dipendente (clocc/port/sys.lisp):
(defun arglist (fn)
"Return the signature of the function."
#+allegro (excl:arglist fn)
#+clisp (sys::arglist fn)
#+(or cmu scl)
(let ((f (coerce fn 'function)))
(typecase f
(STANDARD-GENERIC-FUNCTION (pcl:generic-function-lambda-list f))
(EVAL:INTERPRETED-FUNCTION (eval:interpreted-function-arglist f))
(FUNCTION (values (read-from-string (kernel:%function-arglist f))))))
#+cormanlisp (ccl:function-lambda-list
(typecase fn (symbol (fdefinition fn)) (t fn)))
#+gcl (let ((fn (etypecase fn
(symbol fn)
(function (si:compiled-function-name fn)))))
(get fn 'si:debug))
#+lispworks (lw:function-lambda-list fn)
#+lucid (lcl:arglist fn)
#+sbcl (sb-introspect:function-lambda-list fn)
#-(or allegro clisp cmu cormanlisp gcl lispworks lucid sbcl scl)
(error 'not-implemented :proc (list 'arglist fn)))
EDIT: notare che arity in CL non è realmente un numero, poiché le funzioni Lisp possono accettare gli argomenti optional, rest e keyword in aggiunta a required; questo è il motivo per cui la precedente funzione arglist
restituisce lo lambda list della funzione argomento, non un numero.
Se siete interessati solo a funzioni che accettano solo i parametri richiesti, si avrebbe bisogno di usare qualcosa di simile
(defun arity (fn)
(let ((arglist (arglist fn)))
(if (intersect arglist lambda-list-keywords)
(error "~S lambda list ~S contains keywords" fn arglist)
(length arglist))))
sembra essere implementazione-dipendente. Vedi https://groups.google.com/forum/#!msg/comp.lang.lisp/0WoivuykcKM/0SnbqcFyNogJ –