我正在使用NestWhileList,因为它经常达到“最大评估次数”。在获得一些古玩结果后,我仔细研究了NestWhileList
如何对指定的最大结果数作出反应:
Table[{nmax,
Length@NestWhileList[
(* f: nesting function *) Identity,
(* initial state *) 1,
(* test function *) False &,
(* m: of arguments for test *) 1,
(* nmax: max # applications of f *) nmax,
(* n: extra evaluations *) 1]}, {nmax, 0, 2}];
ToString[TableForm[%,
TableHeadings -> {None, {"nmax", "output length"}}]]
令人惊讶的是,nmax=1
被挑选出来:此处f
应用了2次,而对于所有其他值,它仅应用一次:
nmax output length
0 2
1 3
2 2
“额外评估”似乎是问题的一部分。离开这个选项可以得到更合理的结果:
Table[{nmax,
Length@NestWhileList[
(* f: nesting function *) Identity,
(* initial state *) 1,
(* test function *) False&,
(* m: of arguments for test *) 1,
(* max: max # applications of f *) nmax]},{nmax,0,2}];
ToString[TableForm[%,TableHeadings->{None, {"nmax","output length"}}]]
Out[123]=
nmax output length
0 1
1 1
2 1
我的问题:这是否有意义,或者只是一个错误?
答案 0 :(得分:4)
这没有意义,我很确定这只是一个错误。 NestWhile
同样受到影响:
In[53]:= NestWhileList[# + 1 &, 1, False &, 1, 1, 1]
Out[53]= {1, 2, 3}
In[54]:= NestWhile[# + 1 &, 1, False &, 1, 1, 1]
Out[54]= 3
以下是NestWhileList
的解决方法函数:
myNestWhileList[f_, expr_, test_, m_, max_, n_] :=
Module[{nwl},
nwl = NestWhileList[f, expr, test, m, max];
Join[nwl, Rest[NestList[f, Last[nwl], n]]]
]
In[75]:= myNestWhileList[# + 1 &, 1, False &, 1, 1, 1]
Out[75]= {1, 2}
显然,它不是NestWhileList
的完全替代品,但如果有必要,它应该很容易概括。
我已提交错误报告。