Page 1 of 1

get parameters name of a function

Posted: Thu Jul 05, 2012 6:39 am
by mparsa
Is there any function that gets the name of the function and returns the name of the parameters ?
For example I have a function like this :
(defun test (m n))
I want to get m and n. But I don't want the value of the n and m, Just the name of the parameter "m" and "n".

Re: get parameters name of a function

Posted: Thu Jul 05, 2012 7:11 am
by Goheeca
For a human readable output is the function describe.
Otherwise I recommend you to write a macro for that.
Here is a simple sketch:

Code: Select all

(defmacro my-defun (fname (&rest args) &body body)
  `(symbol-macrolet ((lambda-expr ',args))
      (defun ,fname ,args ,@body)))
Watch out! This is only a sketch it's not a hygienic macro and does a multiple evaluation of its arguments. It's only for defun construct and not for example lambda.
Usage:

Code: Select all

(my-defun plus (a b c)
  (format t "~a ~a" (+ a b c) lambda-expr))

Re: get parameters name of a function

Posted: Thu Jul 05, 2012 7:34 am
by Goheeca
Of course, you can use let instead of symbol-macrolet and it's more suitable.

Re: get parameters name of a function

Posted: Thu Jul 05, 2012 6:39 pm
by nuntius
The CL standard does not specify any functions for this type of introspection.

Several CL implementations provide support for it. Search for "defimplementation arglist" in Slime's sources to see how.