In Mathematica, why is it that a replacement in a recursive function doesn't terminate?
Imagine I've defined a recursive factorial in Mathematica, like this:

    Clear[fact]
    fact[0] = 1
    fact[n_] := n fact[n - 1]
Evaluating fact[10] confirms that the function works and terminates.

A bit of a staple example, but it serves its purpose in this question. Actually, my question pertains to recursive function definitions in general anyway.

I expected evaluating the following replacement to terminate as well:

    x fact[x-1] /. x -> 2

Alas, it runs in to a recursion depth limit:

    $RecursionLimit::reclim: Recursion depth of 256 exceeded.

I expected to see something like:

    2 fact[2-1]

or just the value

    2

My question is twofold:

1. Even with the built-in help and searching the net for clues, I can't explain why Mathematica insists in, apparently, keeping the symbolic result, rather than evaluating the 'intermediate' results and terminating nicely. Who ventures a viable explaination?

2. How can I convince Mathematica to perform according to my expections?

I'm really puzzled by this one, and I really hope someone out there can help me out.

/Twan



In Mathematica, why is it that a replacement in a recursive function doesn't terminate?
===

Imagine I've defined a recursive factorial in Mathematica, like this:

    Clear[fact]
    fact[0] = 1
    fact[n_] := n fact[n - 1]
Evaluating fact[10] confirms that the function works and terminates.

A bit of a staple example, but it serves its purpose in this question. Actually, my question pertains to recursive function definitions in general anyway.

I expected evaluating the following replacement to terminate as well:

    x fact[x-1] /. x -> 2

Alas, it runs in to a recursion depth limit:

    $RecursionLimit::reclim: Recursion depth of 256 exceeded.

I expected to see something like:

    2 fact[2-1]

or just the value

    2

My question is twofold:

1. Even with the built-in help and searching the net for clues, I can't explain why Mathematica insists in, apparently, keeping the symbolic result, rather than evaluating the 'intermediate' results and terminating nicely. Who ventures a viable explaination?

2. How can I convince Mathematica to perform according to my expections?

I'm really puzzled by this one, and I really hope someone out there can help me out.

/Twan


