newLISP Fan Club

Forum => Anything else we might add? => Topic started by: newdep on February 28, 2004, 03:07:31 PM

Title: Quine
Post by: newdep on February 28, 2004, 03:07:31 PM
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.
Title:
Post by: Lutz on February 28, 2004, 08:11:48 PM
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
Title:
Post by: newdep on March 01, 2004, 06:25:50 AM
:-)
Title:
Post by: Lutz on March 01, 2004, 08:45:11 AM
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
Title:
Post by: newdep on March 01, 2004, 09:03:20 AM
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.
Title:
Post by: pjot on May 06, 2005, 02:22:50 PM
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. 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
Title:
Post by: Lutz on May 06, 2005, 04:18:28 PM
same idea as Peter's but shorter:

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

(quine)


Lutz
Title:
Post by: pjot on May 06, 2005, 04:34:44 PM
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
Title:
Post by: Elica on March 07, 2008, 10:14:53 AM
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:
Title:
Post by: pjot on March 07, 2008, 12:16:02 PM
Nice thoughts but disqualified as can be read here: 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
Title:
Post by: Elica on March 07, 2008, 12:20:18 PM
That's why I started my post with the word "IF".
Title:
Post by: pjot on March 07, 2008, 12:26:39 PM
You're tricky ;-)
Title:
Post by: Elica on March 07, 2008, 12:35:36 PM
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.
Title:
Post by: pjot on March 07, 2008, 12:42:28 PM
Still we don't know what a 'program' actually is... Also, the 'NOP' does not do anything, does it? It means 'No OPeration'.
Title:
Post by: Elica on March 07, 2008, 10:07:24 PM
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).
Title:
Post by: newdep on April 08, 2008, 04:07:41 AM
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 :-);;