get parameters name of a function

Discussion of Common Lisp
Post Reply
mparsa
Posts: 26
Joined: Mon Jun 25, 2012 6:15 am

get parameters name of a function

Post by mparsa » Thu Jul 05, 2012 6:39 am

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

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: get parameters name of a function

Post by Goheeca » Thu Jul 05, 2012 7:11 am

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))
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: get parameters name of a function

Post by Goheeca » Thu Jul 05, 2012 7:34 am

Of course, you can use let instead of symbol-macrolet and it's more suitable.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: get parameters name of a function

Post by nuntius » Thu Jul 05, 2012 6:39 pm

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.

Post Reply