Hints for the Challenge:
Below, you can find another hint for the
Challenge.
You should only get here after reading, digesting, and applying the
.
Hint
To summarize,
our program consists of the following ingredients.
- At the lowest level, it is a sequence of characters,
which can be viewed as describing the following larger elements.
- One data structure (an information carrier).
(In our case, a string, i.e. a sequence of characters, named S.)
- This data structure is initialized to a (very special, see next item)
value,
which you could call the blueprint.
(In our case, it has the form of a string expression consisting of
several string literals concatenated by plus operators.)
- The value for the data is chosen such that it encodes the entire
program (hence the name blueprint),
except that it has a hole for the expression that initializes
the data structure
(this expression can also be obained from that same blueprint).
- A data processing part that processes the data in
two ways:
- It can write the data such that it is formatted as a
program expression whose value equals the data, and the format
equals the format in which the initializing expression appears
in the program.
In other words, it copies the blueprint faithfully
(without caring about the plan that is encoded).
(In our case, this is done by the function copy().
Note that it copies everything, starting from index 0.)
- It can write the data such that it produces a multiline text
(through embedded newline markers) in which it inserts a copy of
the data at one appropriate location.
In other words, it executes (the plan expressed by) the blueprint.
(In our case, this is the main program,
which transcribes the data starting at index 4, and
copies the data in at location 14.)
- Both ways of processing rely on the statements
write(), writeln(),
and writequ() to produce
a character sequence as output.
This resembles the way a cell reproduces:
- A cell is built from proteins,
which in turn consist of folded chains of amino acids.
- The cell's DNA contains a blueprint of the entire cell.
DNA consists of (one or more chromosomes, each consisting of)
a double helix of A, C, G, and T nucleotides.
- The cell contains (protein-based) mechanisms
- to replicate its DNA;
- to transcribe its DNA and construct protein structures
for itself and its copy.
There are also some important differences:
These ideas can be expressed more clearly in the program:
- It would be better to put the main program also in a function,
aptly named transcribe().
- It would be better to have both functions work on parameters,
rather than have them work on the global variable S.
- It would be appropriate to give string S a better name,
such as DNA, or even better Genes, or
Blueprint.
- There are still some awkward literals (like 3 and 14) in the program.
If you have not followed the evolution of this program but see it for
the first time, then you will have a hard time understanding why these
specific values appear.
This also makes it difficult to adapt the program.
- We cannot extend the program arbitrarily,
because we must (painstakingly) avoid quotes and also vertical bars.
(Remember that we have been bitten by that before.)
One reason to extend the program is to have it do more than just output
itself (compare this to living organisms who do more than just reproduce).
Can you develop an improved solution to meet these new desires?
Try your program in the
machine,
and see if you can extend it to an improved solution.
Apply the Hint
It is your turn ...
Need Another Hint?
If you tried this hint, or do not see how it helps,
then you are ready for the
.
Copyright ©2009, Tom Verhoeff,
T.Verhoeff@TUE.NL