Newlines added for clarity:
(a(:^)*(!()):#(}:(:)~^a((::*:**:*)*)~*(~*)*~(*)~^a*(*)*{)>(0)(!()){:^}(1)(){:^}(2)(:*){:^}(
3)(::**){:^}(4)(:*:*){:^}(5)(::*:**){:^}(6)(:*::**){:^}(7)(::*::***){:^}(8)(:*:*:*){:^}(9)(
::**::**){:^}R^^S):^
I gave myself the restriction that it must read from STDIN rather than from the top of the stack like an Underload answer typically would. I also used proper, decimal input, which makes up most of the code.
Explanation:
I wrap the program up in a quine-wrapper: (a(:^)*
and ):^
. This means all code inside the quine wrapper will have the program's source code at the bottom of the stack.
In order to convert digits into a normal Church numeral, I use the technique of replacing each digit with the code to multiply by 10 and add that digit:
0 -> (::*:**:*)*
1 -> (::*:**:*)*(:)~*(*)*
2 -> (::*:**:*)*(::)~*(**)*
3 -> (::*:**:*)*(:::)~*(***)*
There's a lot of repetition here, so let's package it up into a subprogram that will take a Church numeral from the top of the stack and use it to construct the "digit string:"
:(:)~^a((::*:**:*)*)~*(~*)*~(*)~^a*(*)*
I put this into a new environment so that it can be accessed quickly:
#(}:(:)~^a((::*:**:*)*)~*(~*)*~(*)~^a*(*)*{)>
Now I can create the replacement code for R
. R
uses the top elements of the stack to form a lookup table to replace a string from STDIN with Betaload code. It works like this:
()
(0)(code for 0)
(1)(code for 1)
(2)(code for 2)
...
R
However, we can use the subprogram we just made to generate the code segments:
(0)(!()){:^}
(1)(){:^}
(2)(:*){:^}
(3)(::**){:^}
...
When R
is run, it will transform the input into a series of subprograms that build up a Church numeral. When this subprogram is executed, it creates that Church numeral on the next element on the stack (0, which was placed down earlier). This means that, after R^
, the top value on the stack will be the Church numeral. We then ^
one more time to apply the Church numeral to the final element in the stack (the program's source code) to get the answer.
Fun fact: I've had the MD for this submission for several months. I'd kept it after misunderstanding a question (that I can't seem to find anymore). I had to dig it up from my Recycle Bin to post it here.
Nice first Question – Bald Bantha – 2017-05-18T21:39:56.000
Can
x
be0
, or can we restrict it to positive integers? – mbomb007 – 2017-05-18T21:58:19.583Is reading the source of our function permitted? – Shaggy – 2017-05-18T22:00:21.630
@mbomb007, I've made an edit to clarify the rules,
x
maybe any whole integer larger than or equal to 0 – Dignissimus - Spammy – 2017-05-18T22:03:19.980@Shaggy, I thought that was a standard loophole, it seems as if it was not. I've made an edit to the question to clearly state that a programme may not read any part of the part of its source code – Dignissimus - Spammy – 2017-05-18T22:09:17.417
I think the consensus for quines is that reading of source code is not permitted by default. I always hold out hope, though, because, without reading the source, creating quines make my brain itchy! – Shaggy – 2017-05-18T22:13:11.957
I disagree with the reading of sourcecode, as it "Defeats the purpose" of a quine. It's easy to
cat $0
, it's difficult to programmatically create yourself. – ATaco – 2017-05-19T01:13:25.903@Spammy23: It's not a standard loophole, but it is a default rule for [tag:quine] challenges, so it was disallowed all along. That said, given that this question is solvable via universal quine constructors, I'm not sure how much it adds to existing quine challenges. – None – 2017-05-19T01:44:17.953
Related, but not a duplicate because the problem is significantly changed by not having to handle negative inputs. – None – 2017-05-19T04:58:45.683
"Your programme may also not directly read any part of its source code". So I guess using a Rexx function called Sourceline(x) which returns line x in the source is a no-no. :) – theblitz – 2017-05-19T08:29:47.557
I'm surprised this isn't a duplicate. – aebabis – 2017-05-19T19:37:42.457
@Spammy23 Does that mean no string literals? :P – SuperJedi224 – 2017-05-22T13:03:11.143