# Trouble with Replacement for base case of recursive function [closed]

I have defined a recursive function W[g_,n_], with the base case values W[0,3] and W[1,1].

Now I want to total W[g,n] for fixed values of 2g-2+n, which I do like so:

pairs[k_] := Solve[2 g - 2 + n == k && g >= 0 && n > 0, {g, n}, Integers]

S[m_]:= Total[W[g, n] /. pairs[m - 1]]


This seems to be working, except when I compute S[2], which should be W[1,1]+W[0,3]. Instead I get zero.

I see that both

W[g, n] /. {g -> 0, n -> 3}
W[g, n] /. {g -> 1, n -> 1}


give me zero, when I have defined these values, and they are not zero.

Your function S[m] isn't well defined. The function pairs[k] returns a list of rules for g and n, so W[g,n] does not know what are the values for them.
S[m_] := Total@(W[Sequence @@ #] & /@ ({g, n} /. pairs[m - 1]))

• Because, g and n are variables of the function W not parts of an expression given by W[g,n]. On the other hand, what you mean with "all other values of g and n"? – José Antonio Díaz Navas Apr 13 '18 at 19:09