Using the Python Interpreter as a Reference
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue Nov 29 03:12:09 EST 2011
On Tue, 29 Nov 2011 13:57:32 +1100, Chris Angelico wrote:
> I'm inclined toward an alternative: explicit recursion. Either a
> different syntax, or a special-case on the use of the function's own
> name, but whichever syntax you use, it compiles in a "recurse" opcode.
> That way, if name bindings change, it's still going to recurse -
> something few languages guarantee, and therefore few languages can
> optimize.
As I recall, Forth uses (or used) a special RECURSE word which turned on
the recursion bit while compiling, so that the compiled word could see
itself.
By memory, the (incomplete) definition:
: fact dup 1- fact * ;
would fail, unless you happened to already have another word called fact
existing at compilation time. To make it recurse correctly, the compiler
needs to make sure that the namespace fact sees includes itself:
RECURSE : fact dup 1- fact * ;
which should work, apart from the embarrassing fact that I don't recall
the syntax for conditional jumps and so the recursion never terminates.
:)
--
Steven
More information about the Python-list
mailing list