quick python expression evaluator

Dave Long dl at silcom.com
Sun Apr 25 14:48:54 EDT 2004


> - build something recursive
>   - how about factorial?

Factorial would be a good one -- it's
a "nice" recursive function because it
can be split up into a part generating
an intermediate structure, and a part
evaluating a computation over it*, so
the outliner could fold up to display
only the final result, or unfold with
each representation of intermediates.

Computing a factorial can be kind of
like lost-wax casting.  To create the
equivalent of a wax model, one builds
a list of descending integers:

    n : n-1 : n-2 : ... : 1 : []

and instead of pouring molten metal
into the mold, replacing everything
that was wax with solid metal, one
makes the following replacements:

	numbers with themselves
	:       with *
	[]      with 1          

yielding a list-like casting:

    n * n-1 * n-2 * ... * 1 * 1

that evaluates to n!.

Eval/apply-like tangled recursion might
not be so handy to display, because it
can't be resolved into the two separate
passes.  I guess a stack trace would be
about the best one could do, even though
it has a bit more operational feel than
functional.

-Dave

:: :: ::

* hylomorphism.  see also:

"The Seasoned COBOLer"
<http://www.ai.mit.edu/~gregs/ll1-discuss-archive-html/msg02770.html>

"Re: primitive circuit optimizer"
<http://lists.canonical.org/pipermail/kragen-discuss/2003-February/000900.html>


More information about the Kragen-discuss mailing list