I like the family of programmming languages, recursion, and statements that refer to themselves.
To warm up, how about the following jokes?
Notice that these two statements give out some information about the speaker, although they do not refer to themselves directly.
Now, what about the following statement?
Is it true or false? If it is, it's not, and if it's not, it is. So, it's neither - it just doesn't make sense (details omitted...)
This statement is vastly different from the one above: you can easily show that it is false.
The following are five pieces of code which reproduce themselves.
The first is in
The second is for
Emacs Lisp and will print itself when
The third is for
Common Lisp, and will print itself when
evaluated too, demonstrating the power and obscurity of the
The last two are for an arbitrary Lisp (or Scheme):
or, if your lisp has backquote, you can write the same shorter:
To check it, just evaluate the following. It should evaluate to
(let ((z '(let ((a '(list 'let (list (list 'a (list 'quote a))) a))) (list 'let (list (list 'a (list 'quote a))) a)))) (equal z (eval z)))
If you were reading this in Emacs/w3, it would have been particularly easy to check.
Another example is
with backquote it is even shorter:
(let ((z ((lambda (x) (list x (list 'quote x))) '(lambda (x) (list x (list 'quote x)))))) (equal z (eval z)))
Note that the last group of examples (4/4*,5/5*) are vastly
superior to the first group.
These expressions do not just print themselves.
They return themselves when evaluated, as lists
Lisp programs are lists of symbols, numbers, arrays and
other objects, not just text files, as in
Actually, this is not the shortest program that does it. The symbol
T evaluates to itself, so it can qualify as a piece of
self-replicating code. With
C you can go even further: an
empty file is a valid
C program which does nothing!
|Sam Steingold<firstname.lastname@example.org>||created: 1995-01-01|