在LISP中可以访问函数的形式吗? [英] In LISP is it possible to access a function's form?
问题描述
(defun x(y)(1+ y));;编辑:我的第一个例子太复杂了
是否有可能将函数x强制到列表中如:
(x(y)(1+ y))
预先感谢!
PS - @ Danlei的例子在Clozure CL中有一个特殊的标志,但是没有人知道如何让功能-LAMBDA-EXPRESSION在SBCL中工作?
://www.lispworks.com/documentation/HyperSpec/Body/f_fn_lam.htmrel =noreferrer> FUNCTION-LAMBDA-EXPRESSION : (function-lambda-expression#'foo)
不保证能够正常工作(......在所有情况下,实现都可以自由地返回nil,true,nil ......)。例如在CCL中:
p> CL-USER> (setq ccl:* save-definitions * t)
T
CL-USER> (defun x(x y)(+ x y))
X
CL-USER> (函数-Lambda表达式#'x)
(LAMBDA(XY)(DECLARE(CCL :: GLOBAL-FUNCTION-NAME X))(BLOCK X(+ XY)))
NIL
在SBCL中,您可以尝试(setq sb-ext:*评估者模式*:解释)
(未测试)。也许还有其他方法可以在SBCL中实现这一点(你可以寻找类似于 * save-definitions *
或者甚至尝试不同的 OPTIMIZE
设置),但我不知道它们。请注意,在将 * evaluateator-mode *
设置为:interpret
之后,将不会编译进入REPL的函数。你可能会遇到更糟的表现。
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?
You could try FUNCTION-LAMBDA-EXPRESSION:
(function-lambda-expression #'foo)
But it's not guaranteed to work ("… implementations are free to return ``nil, true, nil'' in all cases …").
For example in CCL:
CL-USER> (setq ccl:*save-definitions* t)
T
CL-USER> (defun x (x y) (+ x y))
X
CL-USER> (function-lambda-expression #'x)
(LAMBDA (X Y) (DECLARE (CCL::GLOBAL-FUNCTION-NAME X)) (BLOCK X (+ X Y)))
NIL
X
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 different OPTIMIZE
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.
这篇关于在LISP中可以访问函数的形式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!