在 On Lisp (第 9 页),可以找到以下声明:
Functions are the building-blocks of Lisp programs. They are also the building-blocks of Lisp. In most languages the + operator is something quite different from user-defined functions. But Lisp has a single model, function application, to describe all the computation done by a program. The Lisp + operator is a function, just like the ones you can define yourself. In fact, except for a small number of operators called special forms, the core of Lisp is a collection of Lisp functions. What’s to stop you from adding to this collection? Nothing at all: if you think of something you wish Lisp could do, you can write it yourself, and your new function will be treated just like the built-in ones.
我的问题是像
+
这样的东西究竟会怎样?运算符可以使用以下特殊运算符来实现吗?或者实际上是否有更多的运算符被使用而 Graham 只是不够精确和戏剧化?block let* return-from
catch load-time-value setq
eval-when locally symbol-macrolet
flet macrolet tagbody
function multiple-value-call the
go multiple-value-prog1 throw
if progn unwind-protect
labels progv
let quote
有没有办法查看这些函数的源代码?
最佳答案
他并不是说每个功能都是按照这些特殊形式实现的。
他在说 +
(像所有其他函数一样)是一个函数:
(+ x y z)
调用它(其中 +
是函数, x
、 y
、 z
是参数)。 (f (g) (h))
将同时调用 g
和 h
调用前 f
,即使 f
恰好是+
. funcall
调用它和 apply
:(let ((x #'+)) (funcall x 1 2 3))
是 6。关键是这些属性不一定适用于特殊形式。
if
不首先评估其所有参数;你不能引用 let
并间接调用它;等等。当然,这仍然为“编译器魔法”敞开了大门。在某些时候
+
必须执行依赖于例如的低级操作关于如何实现数字。根据您的 Lisp 编译器,细节看起来会有所不同。
关于function - "+"如何在 Common Lisp 中使用特殊运算符/表单实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50121024/