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))))

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

查看所有标签

猜你喜欢:

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

群智能算法及其应用

群智能算法及其应用

高尚 / 中国水利水电出版社 / 2006-5 / 25.00元

《群智能算法及其应用》系统地描述了蚁群算法和粒子群优化算法的理论和实现技术及其应用,简单地介绍了鱼群算法。《群智能算法及其应用》着重强调各种算法的混合,讨论了蚁群算法与模拟退火算法的混合、蚁群算法与遗传算法的混合、蚁群算法与混沌理论混合、模拟退火算法、遗传算法与粒子群优化算法混合、混沌理论与粒子群优化算法的混合以及蚁群算法与粒子群优化算法的混合。书中还讨论了群智能算法在旅行商问题、武器一目标分配问......一起来看看 《群智能算法及其应用》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具