Suppose I define a function globally:
(defun x (y) (1+ y)) ;; Edit: my first example was too complicated
Is it possible to "coerce" the function x into a list like:
(x (y) (1+ y))
Thanks in advance!
PS - @Danlei's example works in Clozure CL with a special flag, however does anyone know how to get FUNCTION-LAMBDA-EXPRESSION to work in SBCL?
In Common Lisp, you might be able to recover the definition of a function using
function-lambda-expression
(see the HyperSpec) or in some implementationsuncompile-function
.When I was spending time on a project to do significant function manipulation, it was easiest to do this sort of thing:
First the list form consisting of '(lambda foo (x ) bar) would be assigned, then I would compile Foo and assign it to the compiled-ojb slot.
You could try FUNCTION-LAMBDA-EXPRESSION:
But it's not guaranteed to work ("… implementations are free to return ``nil, true, nil'' in all cases …").
For example in CCL:
In SBCL, you might try
(setq sb-ext:*evaluator-mode* :interpret)
(untested). Maybe there are other ways to achieve this in SBCL (you might look for an analog of*save-definitions*
or even try differentOPTIMIZE
settings), but I don't know about them. Beware that functions entered in the REPL won't be compiled after setting*evaluator-mode*
to:interpret
, so you will probably experience worse performance.