Newlines added for clarity:
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.
I wrap the program up in a quine-wrapper:
):^. 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:"
I put this into a new environment so that it can be accessed quickly:
Now I can create the replacement code for
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)
However, we can use the subprogram we just made to generate the code segments:
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.