English 中文(简体)
如何评价复读的宏观定义
原标题:How do recursive macro definitions get evaluated

This recursive definition of a macro does what it should (sum integers from 1 to n):

(defmacro sum-int-seq (n)
  `(cond
     ((equal 0 ,n) 0)
     (t (+ ,n (sum-int-seq (- ,n 1))))))

For example (sum-int-seq 5) gives 15.

但它为什么发挥作用? 宏观扩大后,即:

(macroexpand  (sum-int-seq 5))
(IF (EQUAL 0 5) 0 (+ 5 (SUM-INT-SEQ (- 5 1))))

但是,由于对等元是宏观评估,宏观评价应当成为一个无限的循环。 汇编者是否反而建立了休养职能? 如果这一定义产生退学功能,那么对宏观的重新定义是否有任何办法?

(为了简明扼要,这一职能当然会更好发挥作用)

最佳回答

你的榜样并不奏效。

它可以口译员工作。 但是,在汇编过程中,有一位汇编者,你看到了一种无休止的循环。

CL-USER 23 > (defun test (foo)
                (sum-int-seq 5))
TEST

允许使用LspWorks口译员:

CL-USER 24 > (test :foo)
15

让我们努力汇编这一职能:

CL-USER 25 > (compile  test)

Stack overflow (stack size 15997).
  1 (continue) Extend stack by 50%.
  2 Extend stack by 300%.
  3 (abort) Return to level 0.
  4 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.

因此,现在的下一个问题是:它为什么以口译工作,但汇编者却无法编纂?

Okay, Ill予以解释。

首先请看口译人员。

  • it sees (sum-int-seq 5).
  • it macroexpands it to (COND ((EQUAL 0 5) 0) (T (+ 5 (SUM-INT-SEQ (- 5 1))))).
  • it then evaluates above form. It determines that it needs to compute (+ 5 (SUM-INT-SEQ (- 5 1))). For that it needs to macroexpand (SUM-INT-SEQ (- 5 1)).
  • eventually it will expand into something like (cond ((EQUAL 0 (- (- (- (- (- 5 1) 1) 1) 1) 1)) 0) .... Which then will return 0 and the computation can use this result and add the other terms to it.

口译采用该守则,评估其能力,必要时进行宏观推广。 随后对产生的法典进行评估或宏观扩大。 因此。

现在请看汇编者。

  • it sees (sum-int-seq 5) and macroexpands it into (COND ((EQUAL 0 5) 0) (T (+ 5 (SUM-INT-SEQ (- 5 1))))).
  • now the macroexpansion will be done on the subforms, eventually.
  • the compiler will macroexpand (SUM-INT-SEQ (- 5 1)). note that the code never gets evaluated, only expanded.
  • the compiler will macroexpand (SUM-INT-SEQ (- (- 5 1) 1)) and so forth. finally you ll see a stack overflow.

汇编者行走(积极汇编/扩充)守则。 它可能不执行该守则(除非它确实优化或实际上对它进行明确的评估)。

如果是复健的宏观,你就不得不实际减少。 如果你在宏观范围内穿透,那么如<代码>(sum-int-seq 5)可发挥作用。 但是,关于<代码>(fun foo)(n)(sum-int-seq n),这是没有希望的,因为汇编者不知道N的价值是什么。

问题回答

One other thing to add: in your example, the occurrence of sum-int-seq inside the macro is inside a quoted expression, so it doesn t get expanded when the macro is evaluated. It s just data until the macro is called. And since it is nested inside a cond, at run-time the inner macro only gets called when the condition is true, same as in a regular function.

扩大宏观产品 随后经过评估的《私法》。 要求一项功能将执行转移至当时操作的原已存在的利斯码。 除此以外,两者大致相同,再入侵也以同样的方式运作。 尤其是,宏观扩张出于同样的原因,即适当的书面休养功能停止了:因为有终止条件,而且发出呼吁和发出呼吁之间的转变已经写成,以便实际达到这一条件。 如果达到顶点,宏观扩张就会进入 lo,就像不适当的书面补救功能。

To the answer of Kilan I d add, that macroexpand doesn t have to produce a full expansion of all macros in your form, until there s no macro left :) If you look at Hyperspec, you ll see, that it evaluates the whole form until it s not a macro (in your case it stops at if). And during compilation all the macros are expanded, as if macroexpand was applied to each element of the source tree, not only to its root.

这里的实施工作是:

(defmacro sum-int-seq (n)
  (cond
     ((equal 0 n) `0)
     (t `(+ ,n (sum-int-seq ,(- n 1))))))

It is possible to write a recursive macro, but (as was mentioned), the expansion must be able to hit the base case at compile time. So the values of all arguments passed to the macro must be known at compile time.

(sum-int-seq 5)

工作,但

(sum-int-seq n)

不详。





相关问题
Recursive same-table query in SQL Server 2008

I have the following table in a SQL Server 2008 database: Id Name ParentFolder -- ---- ------------ 1 Europe NULL 2 Asia NULL 3 Germany 1 4 UK 1 5 China ...

Finding a class within list

I have a class (Node) which has a property of SubNodes which is a List of the Node class I have a list of Nodes (of which each Node may or may not have a list of SubNodes within itself) I need to be ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签