私の 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)のリストを取得するだけです.....