Deriving a Quine in a Lisp

栏目: IT技术 · 发布时间: 5年前

内容简介:As with myprevious post, this post is another excerpt that will be included in my final Master’s thesis, but I decided it is interesting enough to post it on its own.We start with a definition ofDefinition 1: For an expression

As with myprevious post, this post is another excerpt that will be included in my final Master’s thesis, but I decided it is interesting enough to post it on its own.

We start with a definition of diagonalization (or quotation), as discussed in The Gödelian Puzzle Book :

Definition 1: For an expression Deriving a Quine in a Lisp in which a variable occurs, we say that its diagonalization Deriving a Quine in a Lisp is the substitution of the variable with the quoted expression Deriving a Quine in a Lisp .

This definition allows us to represent self-referential expressions.

For example, let Deriving a Quine in a Lisp . Then, the diagonalization of it will be Deriving a Quine in a Lisp . In Lisp code:

> (define p (lambda (x) (list 'Boro 'is 'reading x)))
> (p 'a-book)
'(Boro is reading a-book)
> (define d (lambda (p x) (p (list 'quote (p x)))))
> (d p 'a-book)
'(Boro is reading '(Boro is reading a-book))

As another example, let Deriving a Quine in a Lisp stand for “Boro is reading the diagonalization of x”, and let Deriving a Quine in a Lisp stand for `Boro is reading the diagonalization of “Boro is reading the diagonalization of x”`. That is, Deriving a Quine in a Lisp . But, the diagonalization of Deriving a Quine in a Lisp is simply Deriving a Quine in a Lisp , i.e. Deriving a Quine in a Lisp . So, Deriving a Quine in a Lisp refers to itself. In Lisp:

> (define q (lambda (x) (list 'Boro 'is 'reading (d identity x))))
> (define r (lambda (x) (d q x)))
> (equal? (d q 'test) (r 'test))
#t

Based on these definitions, we will now show how to derive a Quine , which is a program that when evaluated returns its source code as an output – a metaprogram.

Note how we used (d identity x) earlier, i.e. the diagonalization of x. This simply evaluates to (list 'quote x) .

Now, let’s consider the expression (list x (list 'quote x)) which will return a list with two members: x and its diagonalization:

> (define quine-1 (lambda (x) (list x (list 'quote x))))

What if we apply it to itself?

> (quine-1 quine-1)
'(#<procedure:quine-1> '#<procedure:quine-1>)

Nothing useful. How about applying its quoted version?

> (quine-1 'quine-1)
'(quine-1 'quine-1)

This is exactly why we picked the expression that contains a list of x and its diagonalization. We wanted the evaluation of that expression to return the same expression.

It looks like we are on the right track. Finally, the Quine code that will reproduce itself is just taking the lambda and applying it to its quoted version:

((lambda (x) (list x (list 'quote x)))
  '(lambda (x) (list x (list 'quote x))))

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

高效程序的奥秘

高效程序的奥秘

沃瑞恩 / 冯速 / 机械工业出版社 / 2004-5 / 28.00元

本书适合程序库、编译器开发者及追求优美程序设计的人员阅读,适合用作计算机专业高年级学生及研究生的参考用书。  本书直观明了地讲述了计算机算术的更深层次的、更隐秘的技术,汇集了各种编辑的小技巧,包括常购的任务的小算法,2的幂边界和边界检测、位和字节的重排列、整数除法和常量除法、针对整数的基涵义,空间填充曲线、素数公式等。一起来看看 《高效程序的奥秘》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具