user4813927
user4813927

Reputation:

Convert a FUNCTION to a STRING or SYMBOL in Common Lisp

Is it possible in common lisp to convert a function to a symbol or a string for further use? What i mean is to get a "+" or #:|+| from #'+.

Upvotes: 4

Views: 726

Answers (3)

Soul Clinic
Soul Clinic

Reputation: 1289

(defun function-name (fn)
  (string-downcase (symbol-name (nth-value 2 (function-lambda-expression fn)))))

(function-name #'atom) => "atom"

Upvotes: 0

sds
sds

Reputation: 60014

The only standard way is function-lambda-expression which is not guaranteed to return anything useful.

Neverless, both CLISP and SBCL return the actual function name:

(nth-value 2 (function-lambda-expression #'+))
==> +

or, if you wish,

(symbol-name (nth-value 2 (function-lambda-expression #'+)))
==> "+"

Upvotes: 8

Rainer Joswig
Rainer Joswig

Reputation: 139261

CL-USER> (nth-value 2 (function-lambda-expression #'sin))
SIN

Upvotes: 5

Related Questions