# 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.

## closed as off-topic by José Antonio Díaz Navas, Henrik Schumacher, m_goldberg, MarcoB, b3m2a1Apr 19 '18 at 2:27

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – José Antonio Díaz Navas, Henrik Schumacher, m_goldberg, MarcoB, b3m2a1
If this question can be reworded to fit the rules in the help center, please edit the question.

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]))

• Thank you! But I don't quite understand the reason. Why shouldn't the replacement rule work? And why does it seems to work for all other values of g and n? – PK225 Apr 13 '18 at 19:06
• 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
• I don't have a problem with my code for values other than (g,n) = (1,1) and (g,n) = (0,3). But I suppose that makes sense given what you just said, if a recursive function is interpreted as an expression for values other than the base case. – PK225 Apr 13 '18 at 19:24