Common Lisp:为什么progn是一种特殊形式? [英] Common Lisp: Why progn is a special form?

查看:57
本文介绍了Common Lisp:为什么progn是一种特殊形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Common Lisp的函数参数按从左到右的顺序求值,所以为什么不使用普通函数:

  (defun progn2(& rest body)
(first(last body)))

解决方案

还有 PROGN 的另一个功能使用函数无法获得的代码:



想象一下此代码位于Common Lisp代码文件中:

 (progn 
(defmacro foo()))

vs。

 (我的progn 
(defmacro foo()))

使用 PROGN 编译器将处理 DEFMACRO 表单作为顶级表单。例如,这意味着编译器注意到有一个宏定义,并使其在编译时环境中可用。



使用函数 MY-PROGN ,编译器将无法识别 DEFMACRO 形式,因为它不是顶级形式。 / p>

Since Common Lisp's function arguments evaluate in left-to-right order, why wouldn't use an ordinary function:

(defun progn2 (&rest body)
  (first (last body)))

instead of special form?

解决方案

There is also another feature of PROGN which you can't get with a function:

Imagine this code in a file of Common Lisp code:

(progn
  (defmacro foo () ))

vs.

(my-progn
  (defmacro foo () ))

With using PROGN the compiler will treat the DEFMACRO form as a top-level form. That means for example that the compiler notes that there is a macro definition and makes it available in the compile-time environment.

Using a function MY-PROGN, the compiler won't recognize the DEFMACRO form, because it is not at top-level.

这篇关于Common Lisp:为什么progn是一种特殊形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆