将此翻译为Common Lisp [英] Translating this to Common Lisp

查看:71
本文介绍了将此翻译为Common Lisp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读Olin Shivers的标题为时髦的Lisp编程技术的文章,然后发现那里的第二个示例(标记为"Technique n-1")有点令人费解.它描述了一个自修改宏,如下所示:

I've been reading an article by Olin Shivers titled Stylish Lisp programming techniques and found the second example there (labeled "Technique n-1") a bit puzzling. It describes a self-modifying macro that looks like this:

(defun gen-counter macro (x)
       (let ((ans (cadr x)))   
     (rplaca (cdr x)       
         (+ 1 ans))
     ans))

应该以其调用形式作为参数x(即(gen-counter <some-number>)).这样做的目的是能够执行以下操作:

It's supposed to get its calling form as argument x (i.e. (gen-counter <some-number>)). The purpose of this is to be able to do something like this:

> ;this prints out the numbers from 0 to 9.
  (do ((n 0 (gen-counter 1)))
      ((= n 10) t)
    (princ n))
0.1.2.3.4.5.6.7.8.9.T
>

问题是该函数名称后带有macro符号的语法在Common Lisp中无效.我一直未尝试在Common Lisp中获得类似的行为.有人可以提供CL中类似宏的工作示例吗?

The problem is that this syntax with the macro symbol after the function name is not valid in Common Lisp. I've been unsuccessfully trying to obtain similar behavior in Common Lisp. Can someone please provide a working example of analogous macro in CL?

推荐答案

常见Lisp:

(defmacro gen-counter (&rest x)
  (let ((ans (car x)))   
    (rplaca x (+ 1 ans))
    ans))

但是以上内容只能在解释器中使用,而不能在编译器中使用.

But above only works in the Interpreter, not with a compiler.

使用编译后的代码,宏调用消失了-它被扩展了-无需修改.

With compiled code, the macro call is gone - it is expanded away - and there is nothing to modify.

谨启的读者:您可能想仔细阅读Olin Shivers的论文,非常,并尝试找出他的实际含义...

Note to unsuspecting readers: you might want to read the paper by Olin Shivers very careful and try to find out what he actually means...

这篇关于将此翻译为Common Lisp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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