Convert a FUNCTION to a STRING or SYMBOL in Common Lisp

Viewed 516

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 #'+.

3 Answers

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 #'+)))
==> "+"
CL-USER> (nth-value 2 (function-lambda-expression #'sin))
SIN
(defun function-name (fn)
  (string-downcase (symbol-name (nth-value 2 (function-lambda-expression fn)))))

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