在我的 compsci UIL 类中存在一个挑战问题,即使用尾递归来获取给定数字的二项式系数列表。我想我已经很接近了,但我在基本情况下遇到了困难。
以下是我的代码:
public static Cons binomial(int n)
{
return binomialb(n, null, 1);
}
public static Cons binomialb(int n, Cons last, int power)
{
if(n == power || n < 0)
{
return cons(1, null);
}
else if(last == null)
{
last = cons(1, cons(1, null));
return binomialb(n-1, last, power);
}
else
{
Cons lst = cons(1, null);
while(rest(last)!=null)
{
lst = cons((Integer)first(last)+(Integer)first(rest(last)), lst);
last = rest(last);
}
return binomialb(n-1,lst,power);
}
}
现在我只是得到一个列表(1).....