构造评估的评估等于宏吗? [英] Is evaluating of constructed evaluation equal to macro?

查看:54
本文介绍了构造评估的评估等于宏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道第n个这两个定义是否相等:

I want to know if these two definitions of nth are equal:

I.定义为宏:

(defmacro -nth (n lst)
  (defun f (n1 lst1)
    (cond ((eql n1 0) lst1)
      (t `(cdr ,(f (- n1 1) lst1)))))
  `(car ,(f n lst)))

II.被定义为一堆功能:

II. is defined as a bunch of functions:

(defun f (n lst)
  (cond ((eql n 0) lst)
        (t `(cdr ,(f (- n 1) lst)))))
(defun f1 (n lst)
  `(car ,(f n `',lst)))
(defun --nth (n lst)
  (eval (f1 n lst)))

我有正确的主意吗?宏定义是对表达的评估,是在其主体中构建的吗?

Am i get the right idea? Is macro definition is evaluating of expression, constructed in its body?

推荐答案

好的,让我们从头开始.

OK, let start from the beginning.

宏用于创建通常依赖于宏输入的新表格.在编译或评估代码之前,必须先扩展宏.宏扩展是在评估使用它的表单之前发生的过程.这种扩展的结果通常是Lisp形式.

Macro is used to create new forms that usually depend on macro's input. Before code is complied or evaluated, macro has to be expanded. Expansion of a macro is a process that takes place before evaluation of form where it is used. Result of such expansion is usually a lisp form.

因此,在宏内有几层代码.

So inside a macro here are a several levels of code.

  • 在宏扩展过程中将评估未引用的代码(在运行时不评估!),在您的示例中,您定义了宏扩展时的函数f(用于什么?);
  • 接下来,这里的引号(带有常规引号或反引号,甚至嵌套的反引号)将成为宏扩展结果的一部分(以其字面形式);您可以控制在宏扩展过程中将评估代码的哪一部分以及保持完整(部分或全部引用)的代码.这样一来,任何人都可以在执行之前构造任何东西.
  • Not quoted code will be evaluated during macroexpansion (not at run-time!), in your example you define function f when macro is expanded (for what?);
  • Next here is quoted (with usual quote or backquote or even nested backquotes) code that will become part of macroexpansion result (in its literal form); you can control what part of code will be evaluated during macroexpansion and what will stay intact (quoted, partially or completely). This allows one to construct anything before it will be executed.

宏的另一个功能是,它在扩展之前不评估其参数,而函数则在评估.为了给您一个什么是宏的图片,请看以下内容(刚想到的第一件事):

Another feature of macro is that it does not evaluate its parameters before expansion, while function does. To give you picture of what is a macro, see this (just first thing that came to mind):

(defmacro aif (test then &optional else)
  `(let ((it ,test))
     (if it ,then ,else)))

您可以像这样使用它:

CL-USER> (defparameter *x* '((a . 1) (b . 2) (c . 3) (d . 4)))
*X*
CL-USER> (aif (find 'c *x* :key #'car) (1+ (cdr it)) 0)
4

此宏创建有用的词法绑定,捕获变量it.检查条件后,您不必重新计算结果,可以以"then"和"else"形式访问它.仅用函数是不可能的,它已经在语言中引入了新控件构造.但是,宏不仅仅是创建词汇环境.

This macro creates useful lexical binding, capturing variable it. After checking of a condition, you don't have to recalculate result, it's accessible in forms 'then' and 'else'. It's impossible to do with just a function, it has introduced new control construction in language. But macro is not just about creating lexical environments.

宏是一个功能强大的工具.不可能完全描述您可以使用它做什么,因为您可以做所有事情.但是nth并不是您需要的宏.要构建nth的副本,您可以尝试编写一个递归函数.

Macro is a powerful tool. It's impossible to fully describe what you can do with it, because you can do everything. But nth is not something you need a macro for. To construct a clone of nth you can try to write a recursive function.

请务必注意,LISP宏是编程世界中最强大的功能,而LISP是唯一具有这种功能的语言;-)

It's important to note that LISP macro is most powerful thing in the programming world and LISP is the only language that has this power ;-)

为激励您,我建议您阅读这篇文章: http://www.paulgraham.com/avg. html

To inspire you, I would recommend this article: http://www.paulgraham.com/avg.html

要掌握宏,请从以下内容开始:

To master macro, begin with something like this:

http://www.gigamonkeys.com/book/macros- definition-your-own.html

然后可能是保罗·格雷厄姆(Paul Graham)的论Lisp",然后是放任Lambda".

Then may be Paul Graham's "On Lisp", then "Let Over Lambda".

这篇关于构造评估的评估等于宏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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