A Self-Reproducing Program

February 20, 2009

Write a program that takes no input and generates a copy of its own source text as its complete output.

Pages: 1 2

11 Responses to “A Self-Reproducing Program”

  1. I can’t believe I’d never actually tried to write a quine before. Here’s what I came up with:

    http://pastebin.com/f1c8575a1

    I think my C background is showing, as this may not be particularly Schemish.

    I really wanted the output to be formatted like the original program, though. That proved more difficult than I expected. Perhaps there’s an easier way that I missed.

    http://pastebin.com/f3706d30d

    I used fold to create a q&d add-between function when translating it from PLT Scheme to R6RS. I’m not sure it’s a good implementation or not.

  2. g said
      File "J:\python\reproducing.py", line 1
        File "J:\python\reproducing.py", line 1
    	^
    
    IndentationError: unexpected indent
  3. I do believe g wins the Internet.

  4. slate said

    print open(__file__).read(),

  5. In perl I did:

    system(“type $0”);

    It can’t be that easy, what am I missing??

  6. j0sejuan said

    golang

    
    package main
    
    import (
    	"fmt"
    	"strings"
    )
    
    const code = `
    package main
    
    import (
    	"fmt"
    	"strings"
    )
    
    const code = SOURCE
    
    func main() {
    	fmt.Print(strings.Replace(code, "S" + "OURCE", string(rune(96)) + code + string(rune(96)), -1))
    }
    `
    
    func main() {
    	fmt.Print(strings.Replace(code, "S" + "OURCE", string(rune(96)) + code + string(rune(96)), -1))
    }
    
  7. David said

    In Python:

    str = 'str = @\nprint(str.replace(chr(64),repr(str)))\n'
    print(str.replace(chr(64),repr(str)))
    
  8. V said

    # Slate’s answer In Ruby
    puts File.read(__FILE__)

  9. stutonk said

    In old timey 8-bit BASIC:

    10 LIST
    
  10. stutonk said

    And a real, non-cheater one in Common Lisp

    (DEFVAR D "(DEFVAR D ~S)~%(FORMAT T D D)")
    (FORMAT T D D)
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: