Quine

For the Compleat Fan
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Quine

Post by 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 :-)

Norman.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

this one is short but needs 7.5.4

Code: Select all

(define (make-me) 
  (source 'make-me))
a little bit longer but works on older versions too:

Code: Select all

(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:

Code: Select all

(lambda (x))
Lutz

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

:-)

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

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.

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

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:
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. 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:

Code: Select all

(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:

Code: Select all

(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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

same idea as Peter's but shorter:

Code: Select all

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

(quine)
Lutz
Last edited by Lutz on Fri May 06, 2005 11:57 pm, edited 2 times in total.

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

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
Posts: 57
Joined: Wed Feb 13, 2008 6:41 pm

Post by Elica »

If this is a quine:

Code: Select all

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

Code: Select all

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

Code: Select all

(lambda )
And here is another one:

Code: Select all

nil
And finally:

Code: Select all

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
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

Nice thoughts but disqualified as can be read here: http://en.wikipedia.org/wiki/Quine_%28computing%29
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
Posts: 57
Joined: Wed Feb 13, 2008 6:41 pm

Post by Elica »

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

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

You're tricky ;-)

Elica
Posts: 57
Joined: Wed Feb 13, 2008 6:41 pm

Post by Elica »

pjot wrote: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
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

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

Elica
Posts: 57
Joined: Wed Feb 13, 2008 6:41 pm

Post by Elica »

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

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

This one is probably not valid enough...

[~$]newlisp -e "(map sym (main-args))"
(newlisp -e (map sym (main-args)))


Dont do this!!! ->

newlisp -e "(exec (string (main-args)))"
;; unless youre a happy process killer :-);;
-- (define? (Cornflakes))

Locked