After taking a step back from the given definition, it seems to me that the `Mod[...,2]' aren't quite capturing the real periodicity interval of the four case distinctions that are required here.

A more natural approach would be to use Mod[...,4] and make the case distinction based on the four possible values of the remainder.

If $m$ is an integer, the four cases are that the argument is equal to $2(2m)$ (even), $2(2m)+1$ (odd), $2(2m+1)$ (even) or $2(2m+1)+1$ (odd). The remainders of these under division by $4$ are $0$, $1$, $2$ and $3$. And here, $2m$ is even while $(2m+1)$ is odd. Then you can write down the cases in a (to me) more readable way:

The recursion is now both readable and fast, even without memoization. With recursion relations, readability is more important to me than terseness.

A little more explanation (so that the answer looks less terse...): I implemented the "If statement" in the form of (delayed) rules that depend on the output of the function QuotientRemainder[x, 4]. That output is a list whose first argument is the integer $m$ I introduced earlier, and the second argument is the remainder on which the case distinction is based.

Therefore, the left-hand sides of the rules (e.g., {m_, 0} :> f[2 m]) are tuples, where now m_ appears instead of m because it is a placeholder for the actual value Quotient[x, 4] when f[x] is called. On the right-hand side of the rules, 2 m or 2 m +1 appears in place of the even or odd integer n in the original formulation.

I omitted the h[1] = 1, but with that added, it should be identical.
–
celtschkJun 13 '12 at 16:29

@celtschk I made a ninja edit of my comment; it originally said that I was about to post a similar solution (to your comment version). Then I realized they were not the same.
–
Mr.Wizard♦Jun 13 '12 at 16:31

Ah, OK, I understood your comment to say that my code does not give the same result.
–
celtschkJun 13 '12 at 16:33

Here, the first definition tells Mathematica that whenever it finds the expression f[1], it shall replace it with 1. This part of your definition works as expected.

The second definition tells Mathematica that if it finds any expression which matches the pattern f[2 n_] then it shall replace it with the expression If[Mod[n, 2] == 0, f[n], 2 f[n]] where n has been replaced by whatever expression matched the n_ part. OK, so what does it mean to match that expression? Well, it means that your expression (after being evaluated) has exactly this form. So for example, assuming x was not assigned a value, then if you type f[2 x] then Mathematica will note that if in the pattern f[2 n_]n_ is replaced by x, then it will get f[2 x], therefore it has a match, and therefore replaces the right hand side and continues evaluation.

So what happens with e.g. f[4]? You might be inclined to say that if you replace n with 2, you'd get f[4] from f[2 n_], but that is not true. What you get by this replacement is f[2*2]. Further evaluation of that expression of course would eventually give f[4], but that's not how Mathematica works (and BTW, further evaluation would also include evaluation of f[4] so you'd get an endless loop otherwise, except if making an extra exception for that). Now f[4] is clearly not the same expression as f[2 2], and therefore you don't get a match. Since you don't get a match for the other two definitions, no further evaluation is possible, and you get f[4] as result.

Note that f[2*2] would not invoke that definition either because the argument is evaluated before trying to match the function, and since that evaluation results in 4, there's again no match. You could, however, explicitly tell Mathematica to not evaluate that argument before pattern matching, by using f[Unevaluated[2*2]]. In that case, you will get a match for the second definition, with n being 2. Thus the right hand side would evaluate to f[2] which would be the final result because again there's no matching rule.

Note however that even if Mathematica worked differently, your definition would still not be meaningful because there's no built-in assumption that a variable called n has to be an integer (and any such assumption would be a bad idea anyway, since n could e.g. describe the refractive index of a material).

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.