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

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

查看所有标签

猜你喜欢:

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

连线力

连线力

杨国斌 / 邓燕华 / 广西师范大学出版社 / 2013-9 / 39.00

《连线力》,最关切我们未来的“思想@网络.中国”丛书之一,互联网中国传媒参考书。 中国网民在行动。在中国的广大网民中,普遍存在着对正义的渴望和追求,对弱者和小人物的同情, 对贪官污吏的痛恶,对政府的失望, 对权贵的嘲讽,对沟通的渴望,甚至对革命的呼唤。这些因素有着共同的内在逻辑,即情感逻辑。在这个意义上,情感汹涌的网络事件,是整个中国社会情感结构的脉络。 1994年,中国开通了全功能的......一起来看看 《连线力》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

URL 编码/解码
URL 编码/解码

URL 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换