next up previous contents index
Next: 1.11 Threads Up: 1. Onyx Language Tutorial Previous: 1.9 Error handling   Contents   Index


1.10 Introspection

The following code defines a named procedure that calculates factorials:

#n factorial #result
$factorial {
    dup 1 gt {
        #n-1 factorial
        dup 1 sub factorial
    }{
        # Terminate recursion.
        1
    } ifelse

    mul
} def

What this code actually does is create an executable array, which can be examined and modified. The following transcript shows the effects of modifying the code:

onyx:0> 3 factorial 1 sprint
6
onyx:0> $factorial load 2 sprint
{dup 1 gt {dup 1 sub factorial} {1} ifelse mul}
onyx:0> $factorial load 4 {1 pstack} put
onyx:0> 3 factorial
1
1
2
3
onyx:1> 1 sprint
6
onyx:0>

First $3!$ is calculated. Then the ``else'' clause of the ifelse construct is modified to print the stack and is recalculated. As can be seen, the stack is printed during the calculation.



Jason Evans 2005-03-16