Quine

Started by newdep, February 28, 2004, 03:07:31 PM

Previous topic - Next topic

newdep

Im intrested in a "Quine" in NewLisp ;-) anyone already had his head taken off

constructing one?



Dont peek at http://www.nyx.net/~gthompso/self_lisp.txt">http://www.nyx.net/~gthompso/self_lisp.txt   :-)



Norman.
-- (define? (Cornflakes))

Lutz

#1
this one is short but needs 7.5.4



(define (make-me)
  (source 'make-me))


a little bit longer but works on older versions too:



(define (printme )
  (save "printme" 'printme)
  (read-file "printme"))


both return itself in a string (including line breaks)



or just evaluate this, which is the shorted possible, because in  newLISP lambda expressions evaluate/return to themselves:



(lambda (x))


Lutz

newdep

#2
:-)
-- (define? (Cornflakes))

Lutz

#3
strictly speaking it should probably be:



(define (make-me)

  (print (source 'make-me)))



and similar on the second example, depending on what "self reproducing code" means. In that case thes last example would not be valid.



Lutz

newdep

#4
Yes your right about "self printing code" still i think the (lambda (x)) beats it all ;-) That an advantage .. Its short but good enough to brighten the thoughts on newlisp ;-)



Norman.
-- (define? (Cornflakes))

pjot

#5
Well guys, I do not completely agree with you here... ;-) Since when put in a file things go wrong.



According to Wikipedia, a Quine is this:


Quote
A quine is a program (a form of metaprogram) that produces its complete source code as its only output.

Quoted from http://en.wikipedia.org/wiki/Quine">http://en.wikipedia.org/wiki/Quine. Opening the file and reading it's contents to print is considered cheating.



-------------------------------------------------------------------------



Now, the first example of Lutz is nice, but when put in a file "quine.lsp", and run with "newlisp quine.lsp", things go wrong:



(define (make-me)
  (source 'make-me))

(println (make-me))

This actually prints the lambda, but not the "println" statement below; also it will leave us in the newLisp prompt.



I suggest the following improvement:



(define (make-me)
(println (source 'make-me))
(println "(println (make-me))")
(println "(exit)")
(println ""))


(println (make-me))
(exit)

Now the code will completely produce itself as it's only output, when put in a file. Any improvement on this code is appreciated... :-)



I tried to get things working with 'silent' but for some reason this did not generate the correct result. Is the 'silent' statement only working in interactive mode?



Peter

Lutz

#6
same idea as Peter's but shorter:

(define (quine )
  (println (source 'quine) "(quine)")
  (exit))

(quine)


Lutz

pjot

#7
Well what can I say...



Now this is a real Quine! Great! Can this Quine not be added somewhere with cool newLisp examples? It shows the power of newLisp very well. It shows perfectly how things can be done in a small and efficient way.



Cheers & thanks,



Peter

Elica

#8
If this is a quine:
(lambda (x))

Then here is a little bit shorter one:
(lambda ())

Let's get rid of the pair of parentheses (but we still need the space)
(lambda )

And here is another one:
nil

And finally:
0

But still this is not the shortest quine. The shortest one is the empty program. If you run it you will get on the screen the exact source of the program. Here it is the quinnest quine:

pjot

#9
Nice thoughts but disqualified as can be read here: http://en.wikipedia.org/wiki/Quine_%28computing%29">http://en.wikipedia.org/wiki/Quine_%28computing%29


Quote
Also, a quine that contains no code is ruled out as trivial; in many programming languages executing such a program will output the code (i.e. nothing). Such an empty program once won the "worst abuse of the rules" prize in the Obfuscated C contest.




In the end, the question is: how do we define a program? Can a single number like '0' be called a program?



Peter

Elica

#10
That's why I started my post with the word "IF".

pjot

#11
You're tricky ;-)

Elica

#12
Quote from: "pjot"Can a single number like '0' be called a program?


May be in some machine code for some processor there is an instruction with opcode 0. So, this might be a kind of minimalistic program.



For example, in 80x86 family the number 144 (i.e. 0x90) stands for NOP instruction, which is a program by itself. So, a single number could be a program.

pjot

#13
Still we don't know what a 'program' actually is... Also, the 'NOP' does not do anything, does it? It means 'No OPeration'.

Elica

#14
The functionality of NOP is not the point. There are many others 1-byte instructions, e.g. RET (return from procedure). A single RET is the shortest subroutine.



In some languages there are formal definitions of programs. For example in Pascal there is a program reserved word. In Lisp and Logo (and machine code) the boundary between data and instructions is pretty vague, so I believe that there is a smooth transition between programs and data.



The situation is similar to that -- take the rainbow (spectrum) and ask people to point the beginning and the end of the yellow color. Everyone will show something different.



Let's go back to the quine problem. I think that a fair (i.e. classical) quine program should have at least one instruction for output and no instructions for input. Cases like empty program, 0, (lambda (x)) are ruled out, because they do not print anything, but rely on the programming environment to show the result (i.e. the P in the REPL acronym).