.\" .\" Copyright (c) 2015 Ingo Schwarze .\" .\" Permission to use, copy, modify, and distribute this software for any .\" purpose with or without fee is hereby granted, provided that the above .\" copyright notice and this permission notice appear in all copies. .\" .\" THIS PROGRAM IS NOT EXPECTED TO BE USEFUL FOR ANY PRACTICAL PURPOSE. .\" ITS SOLE INTENTION IS TO DEMONSTRATE SUBTLETIES OF THE ROFF LANGUAGE. .\" .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" .\" -------------------------------------------------------------------- .\" Minimal demonstration of a recursive macro that redefines itself. .\" Note that recursive macros that merely append to their own .\" definition can be demonstrated in much simpler ways. .\" -------------------------------------------------------------------- .\" .\" Arbitrary payload. Not recursive, not self-modifying. .\" For example, show the Fibonacci series. .\" .de payload .nr c \\na+\\nb .if !\\nc .nr c 1 .nr a \\nb .nr b \\nc \\nb .. .\" .\" Setup macro. Recursive, self-expanding. .\" When called once, it recursively expands itself into the step macro. .\" The termination condition for self-expansion is an explicit counter. .\" .de recurse endrec .nr i \\ni+1 .if \\ni<18 .recurse .de recurse endrec \" Scope remains open until .endrec in the caller. .payload .endrec .\" .\" Step macro. Not recursive, self-consuming. .\" Build it by calling the setup macro once and appending .\" the termination condition for self-consumption: .\" At the end of the last invocation, empty both .run and itself. .\" .\" Each time the step macro is executed, it calls the payload once, .\" then redefines itself as needed for the next invocation. .\" .recurse .de run endrun .endrun .de recurse endrec \" Scope remains open until .endrec in the caller. .endrec \" Closes the first scope begun in the setup macro. .\" .\" Main program. Tail-recursive, not self-modifying. .\" Call the step macro until it terminates. .\" .de run endrun .recurse .endrec \" Closes one scope begun in the setup or step macro. .run .endrun .\" .\" Execute the main program. .\" .run