quines


quine: /kwi:n/, n.

(from the name of the logician Willard van Orman Quine, via Douglas Hofstadter). A program that generates a copy of its own source text as its complete output. Devising the shortest possible quine in some given programming language is a common hackish amusement. (We ignore some variants of BASIC in which a program consisting of a single empty string literal reproduces itself trivially.) Here is one classic quine:

 ((lambda (x) 
   (list x (list (quote quote) x))) 
  (quote 
     (lambda (x) 
       (list x (list (quote quote) x))))) 

This one works in nearly all Lisps, including Scheme. It's relatively easy to write quines in languages such as PostScript & Lisps, which readily handle programs as data; much harder (and thus more challenging!) in languages like C, which do not. (From the Jargon File, slightly modified.)

Examples of Quines

Many of these are from the Quine Page. Add your own.

Author: Tanaka Tomoyuki(tanaka@ucdavis.edu) Note: (Chez Scheme Version 5.0b)

  (call/cc 
   (lambda (c) 
           (c ((lambda (c) `(call/cc (lambda (c) (c (,c ',c))))) 
               '(lambda (c) `(call/cc (lambda (c) (c (,c ',c))))))))) 

Author: Tanaka Tomoyuki(tanaka@ucdavis.edu) Note: (Chez Scheme Version 5.0b)

 (call/cc 
   (lambda (c) 
     (call/cc 
       (lambda (cc) 
         (c ((lambda (c) 
               `(call/cc 
                  (lambda (c) (call/cc (lambda (cc) (c (,c ',c))))))) 
             '(lambda (c) 
                `(call/cc 
                   (lambda (c) (call/cc (lambda (cc) (c (,c ',c))))))))))))) 

Author: Tanaka Tomoyuki(tanaka@ucdavis.edu) Note: (Chez Scheme Version 5.0b)

  ((lambda (q qq) ((lambda (x) `((lambda (q qq) ,(q x)) . ,(q qq))) 
                   '(lambda (x) `((lambda (q qq) ,(q x)) . ,(q qq))))) 
   (lambda (q) `(,q ',q)) 
   '(lambda (q) `(,q ',q))) 

Author: Tanaka Tomoyuki(tanaka@ucdavis.edu) Note: (Chez Scheme Version 5.0b)

  ((lambda (c) 
    (if (procedure? c) (c 0) 
        ((lambda (c) `((lambda (c) (if (procedure? c) (c 0) (,c ',c))) 
                       (call/cc call/cc))) 
         '(lambda (c) `((lambda (c) (if (procedure? c) (c 0) (,c ',c))) 
                        (call/cc call/cc)))))) 
   (call/cc call/cc)) 

Author: Tanaka Tomoyuki(tanaka@ucdavis.edu) Note: (Chez Scheme Version 5.0b)

  ((lambda (c) 
    (if (procedure? c) 
        (c '`((lambda (c) (if (procedure? c) (c ',c) ,c)) (call/cc call/cc))) 
        `((lambda (c) (if (procedure? c) (c ',c) ,c)) (call/cc call/cc)))) 
   (call/cc call/cc)) 

Author: Tanaka Tomoyuki

 ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x)) 

Author: Tanaka Tomoyuki

 ((lambda (q) ((lambda (x) `((lambda (q) ,((eval q) x)) ',q)) 
               '(lambda (x) `((lambda (q) ,((eval q) x)) ',q)))) 
  '(lambda (x) `(,x ',x))) 

Author: Moshe Zadka (moshez@math.huji.ac.il) Note: Guile (A GNU butchering of Scheme)

 #!/usr/bin/guile \ 
 -e main -s 
 !# 
 (define (main args) 
    (display program) 
    (write program) 
    (display #\)) 
    (newline)) 
  
 (define program 
 "#!/usr/bin/guile \\ 
 -e main -s 
 !# 
 (define (main args) 
    (display program) 
    (write program) 
    (display #\\)) 
    (newline)) 
  
 (define program 
 ") 

Author: Moshe Zadka (moshez@math.huji.ac.il)

 ((lambda (p) (write (list p (list (quote quote) p)))) 
  (quote (lambda (p) (write (list p (list (quote quote) p)))))) 

Author: Tanaka Tomoyuki Note: Palindromic in nature (look carefully -- I missed it at first)

 ((lambda (x) `(,(reverse x) ',x)) '(`(,(reverse x) ',x) (x) lambda)) 

category-code