function - "+"如何在 Common Lisp 中使用特殊运算符/表单实现

标签 function common-lisp special-form

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) 调用它(其中 + 是函数, xyz 是参数)。
  • 参数将首先被评估:(f (g) (h))将同时调用 gh调用前 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/

    相关文章:

    javascript - 折叠/展开脚本不起作用

    common-lisp - 什么时候使用 “apply”?什么时候使用 “funcall”?

    clojure - 有没有办法以编程方式获取 clojure 特殊表单的集合?

    lisp - 允许将特殊形式视为正常值会产生什么后果?

    c - 结构数组的参数类型不兼容

    c++ - 我怎样才能创建一个声明为字符串的函数,结束该行

    python - 如果我使用自定义字典作为函数的全局变量,为什么我不能访问内置函数?

    lisp - 如何从 plist 中获取属性

    lisp - defstruct 实例化是否评估其 &key 参数?

    function - Clojure 星号特殊形式(fn*、let* 等...)