This challenge is a generalization of the (first) Challenge, which we assume you have completed. It was already briefly stated in the Afterthoughts on the first Challenge. Here is a more complete description of the Generalized Challenge:
Write a program G for Tom's JavaScript Machine
- that takes as input any valid JavaScript definition of a function f that takes a string as argument, and
- that produces as output a valid JavaScript program Pf
- such that the program Pf behaves like the function f applied to the program Pf itself, that is, like f(Pf).
This Generalized Challenge will lead you into the heart of the foundations of theoretical computer science. In particular, at the end of the accompanying hints we will consider a very special function f related to the (hypothetical) halting function H. This function takes as argument a string with a JavaScript program and supposedly returns whether execution of this program, without input, halts.
Notes:
the program Pf output by G is a self-reproducing program, because f(Pf) just outputs Pf; Hence, Pf outputs itself, and thus it is a solution to the first challenge. Therefore, the first challenge is a special case of the new challenge; the new challenge generalizes the first challenge.
Applying the Generalized Challenge to f with definition
yields a program Pf that can serve as Qg
As with the first challenge, you are encouraged to write a program
These requirements could be dropped initially, but using extra JavaScript features is not necessary and can be distracting. User-defined functions can be helpful, as well as the predefined constants QU and NL.
In case you get stuck, you can look at some hints.