23
\$\begingroup\$

​Your task is to write a program or function that outputs the first character of its source code, then the second, then the third... each time it is run.

For example, if your program was foo in language bar in file baz.bar, then you should get output similar to this:

λ bar baz.bar
f
λ bar baz.bar
o
λ bar baz.bar
o

Your program can do anything once its done printing its source code in this fashion. You may modify the source code for the file, but remember that the source code to be printed is the original source code.

This is a code-golf, so the shortest program in bytes wins.

\$\endgroup\$
  • 1
    \$\begingroup\$ If we can modify the source code for the file, does that mean we can read it too? \$\endgroup\$ – FlipTack Dec 21 '16 at 11:29
  • 1
    \$\begingroup\$ @Flp.Tkc Good question. I honestly have no idea. I don't want you to construct the quine by reading the source, but I'm okay if you read the source for the modification of the file only. \$\endgroup\$ – Conor O'Brien Dec 21 '16 at 15:44
6
\$\begingroup\$

Jelly, 12 bytes

“;⁾vṾ®ȯ©Ḣ”vṾ

This is a niladic link. Try it online! (Includes code to call the link twelve times.)

How it works

“;⁾vṾ®ȯ©Ḣ”vṾ  Niladic link.

“;⁾vṾ®ȯ©Ḣ”    Set the left argument and the return value to s =: ';⁾vṾ®ȯ©Ḣ'.
           Ṿ  Uneval; yield r =: '“;⁾vṾ®ȯ©Ḣ”', a string representation of s.
          v   Eval; execute s as a Jelly program with argument r.

 ⁾vV          Yield 'vṾ'.
;             Concatenate r and 'vṾ', yielding q =: '“;⁾vṾ®ȯ©Ḣ”vṾ'.
    ®ȯ        Take the flat logical OR of the register (initially 0) and q.
              This replaces 0 with q in the first run, but it will yield the
              content of the register in subsequent runs.
      ©       Copy the result to the register.
       Ḣ      Head; pop and yield the first character of the register.
              This modifies the string in the register, so it holds one fewer
              character after each call.

As an added bonus, since the register will hold an empty string after the twelveth call, it is once again falsy and the link is ready to start over. Calling the link 24 times will output the source code twice, calling it 36 times thrice, etc.

\$\endgroup\$
  • \$\begingroup\$ I don't know Jelly, so what does the footer do precisely? Why ^17? \$\endgroup\$ – Conor O'Brien Dec 20 '16 at 2:08
  • \$\begingroup\$ Call the link above (¢), identity function (¹, not really needed after the first call), set return value to linefeed (, implicitly prints the previous return value), repeat. ¹ and are both superscripts, but they're unrelated. I've replaced them with the less confusing ¢;¢;¢;¢;¢;¢;¢;¢;¢;¢;¢;¢ (call, concatenate, repeat). \$\endgroup\$ – Dennis Dec 20 '16 at 2:09
12
\$\begingroup\$

Javascript - 26 bytes

Defines f() that returns the source code character by character.

n=0;f=x=>("n=0;f="+f)[n++]

Returns undefined after it runs out of characters.

n=0;f=x=>("n=0;f="+f)[n++]
for(i=0;i<30;i++){console.log(f())} //test harness

\$\endgroup\$
  • \$\begingroup\$ If I'm not mistaken, you have to include the function call as part of the code. \$\endgroup\$ – Mama Fun Roll Dec 20 '16 at 3:09
  • \$\begingroup\$ @MamaFunRoll link to relevant meta post? \$\endgroup\$ – Downgoat Dec 20 '16 at 3:20
  • \$\begingroup\$ I know of none :P However, I've always thought that the function call itself was part of the quine; maybe there's something I'm missing? \$\endgroup\$ – Mama Fun Roll Dec 20 '16 at 4:05
  • \$\begingroup\$ @MamaFunRoll The spec specifically allows functions, so a call should not be needed. It wouldn't make much sense for this particylar challenge anyway. \$\endgroup\$ – Dennis Dec 20 '16 at 4:57
  • \$\begingroup\$ Alrighty, just making sure. \$\endgroup\$ – Mama Fun Roll Dec 20 '16 at 14:19
2
\$\begingroup\$

stacked, noncompeting, 34 bytes

[tostr ':!' + execCounter # out]:!

A variation on the standard quine. This is a full program. This uses execCounter to get how many times this program specifically was run. Errors after outputting everything.

Try it here!

\$\endgroup\$
2
\$\begingroup\$

Pip, 31 bytes

{Y"{Y (yRsRPy++v)}"(yRsRPy++v)}

An anonymous function. Test it on TIO!

Explanation

Start with this standard Pip quine:

Y"Y yRsRPy"yRsRPy
Y"Y yRsRPy"        Yank that string into y
           yRsRPy  In y, replace the space with repr(y)

Wrap this in curly braces to make it a function. Now, instead of returning the whole source, we need to index into it. Using a global variable for the index and incrementing it each time will fulfill the "next character each time it's called" requirement. v is the best candidate because it is preinitialized to -1. Incrementing it the first time gives an index of 0, next time 1, etc.

Pip has cyclical indexing, so once the function prints its last character, it will start over at the beginning.

\$\endgroup\$
1
\$\begingroup\$

Python, 90 bytes

An extension on the standard Python quine (golfing tips welcome):

def f(n=0,s='def f(n=0,s=%r):\n while 1:yield(s%%s)[n];n+=1'):
 while 1:yield(s%s)[n];n+=1

This is a python generator function, meaning that you iterate over it, and each iteration provides the next character in the source code. When all the characters have been returned, this crashes with IndexError.

For testing, simply append this script to the end of the program:

source = ''
try:
    # Call generator, add characters to the string
    for char in f():
        source += char
except IndexError:
    # Generator has reached end of source code
    pass

print(source)

Or try it online!

\$\endgroup\$
1
\$\begingroup\$

*><>, 13 21 bytes

" r:2+a1Fi1+:1F1+[ro;

Creates a file named \n to keep track of the index.

This may be able to be golfed more, but nothing immediately jumps out at me ...

Output

$ starfish incrementalquine.sf 
"$ starfish incrementalquine.sf 
 $ starfish incrementalquine.sf 
r$ starfish incrementalquine.sf 
:$ starfish incrementalquine.sf 
2$ starfish incrementalquine.sf 
+$ starfish incrementalquine.sf 
a$ starfish incrementalquine.sf 
1$ starfish incrementalquine.sf 
F$ starfish incrementalquine.sf 
i$ starfish incrementalquine.sf 
1$ starfish incrementalquine.sf 
+$ starfish incrementalquine.sf 
:$ starfish incrementalquine.sf 
1$ starfish incrementalquine.sf 
F$ starfish incrementalquine.sf 
1$ starfish incrementalquine.sf 
+$ starfish incrementalquine.sf 
[$ starfish incrementalquine.sf 
r$ starfish incrementalquine.sf 
o$ starfish incrementalquine.sf 
;$ starfish incrementalquine.sf

Explanation

" r:2+                 build the quine
      a1F              open file named "\n"
         i1+:          read input, increment by 1, duplicate
             1F        save incremented input to file
               1+[     increment input, copy that many values to a new stack
                  ro   output the character at the beginning of the new stack
                    ;  end

Cheating Incremental Quine

a1Fi1+:0go1F;

Explanation

a1F            open file named "\n"
   i1+         read input and add 1 to it
      :0g      push the value at (input,0) to the stack
         o     output the value
          1F   save the incremented input to the file
            ;  exit
\$\endgroup\$
  • \$\begingroup\$ haha, I had no idea you could name a file "\n". Though this technically reads the source code, by the g instruction. \$\endgroup\$ – Conor O'Brien Dec 21 '16 at 17:49
  • \$\begingroup\$ @ConorO'Brien, ah fair enough. hmm ... \$\endgroup\$ – redstarcoder Dec 21 '16 at 17:51
  • \$\begingroup\$ Does *><> have some sort of stack indexing? Or command repetition? Then, you can use the standard quine framework "your code goes here; for ><>, then access the nth member on the stack \$\endgroup\$ – Conor O'Brien Dec 21 '16 at 18:11
  • \$\begingroup\$ @ConorO'Brien yeah Teal Pelican showed me this quine variant on my other cheating quine answer :). \$\endgroup\$ – redstarcoder Dec 21 '16 at 18:43
  • 1
    \$\begingroup\$ @redstarcoder I've been working more on quines (YAY!) and I figured out a nice trick, if you replace r for # you can :1- the # like; #;or[+1F1:+1iF1a-1:" I cannot test this at the moment but I believe it cuts 1 byte from your code. ><> quines can be done like #o<-1:" etc :D \$\endgroup\$ – Teal pelican Dec 23 '16 at 12:01
1
\$\begingroup\$

Mathematica, 91 bytes

Comments very welcome; I'm still learning the ropes about what quines are proper quines.

(If[!NumberQ[q], q = 0]; StringTake[ToString[#0]<>FromCharacterCode[{91, 93}], {++q}]) & []

Defines a function called repeatedly with no arguments. After the 91st call, it throws a big error and returns unevaluated.

There were two issues to overcome: first, I wanted to just use StringTake[ToString[#0]<>"[]"], but ToString[] seems to erase the quotation marks; so I had to replace "[]" by FromCharacterCode[{91, 93}]. Second, Mathematica variables start out uninitialized, so I can't call ++q before q is defined; this is why the initial If[!NumberQ[q], q = 0] is needed.

Irrelevant coda: while looking up NumberQ, I learned that Mathematica has a function called TrueQ ... which, yes, returns True if the argument is True and False if the argument is False! (The utility is that it returns False on all other arguments as well.)

\$\endgroup\$
1
\$\begingroup\$

Microscript II, 40 33 bytes

A code block literal, the language's closest equivalent to a function:

{ss1K+>K>s#<v{o}sl*v!(123v)lKp<o}

After running, it puts itself back into x to make it easier to invoke again.

\$\endgroup\$
0
\$\begingroup\$

Bash (and zsh, ksh), 39-bytes

a=`<$0`;pwd>>0;echo ${a:`wc -l<0`-1:1}

It prints nothing after once the program is printed out.

Ensure 0 does not exists in the current directory and run:

bash iquine.bash
\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! You've got a clever idea for incrementing the index. Unfortunately, it looks like this answer works by reading its own source code, which means it's not a valid quine by our standards. If you modify it to use quine techniques other than reading its own source, it'll be a good answer. \$\endgroup\$ – DLosc Dec 25 '16 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.