get parameters name of a function

Discussion of Common Lisp

get parameters name of a function

Postby 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".
mparsa
 
Posts: 26
Joined: Mon Jun 25, 2012 6:15 am

Re: get parameters name of a function

Postby 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).
User avatar
Goheeca
 
Posts: 199
Joined: Thu May 10, 2012 12:54 pm

Re: get parameters name of a function

Postby 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).
User avatar
Goheeca
 
Posts: 199
Joined: Thu May 10, 2012 12:54 pm

Re: get parameters name of a function

Postby 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.
User avatar
nuntius
 
Posts: 498
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 2 guests