这段代码:
sub MAIN(Int $N = 128)
{
my @pascal = ((1,), { (0, |@^a) Z+ (|@^a, 0) } ... *);
my @top = @pascal[^$N];
say "Percentage of odd numbers in the first $N rows: ",
(100 × @top».grep(* % 2).sum / @top.sum).fmt('%.12f%%');
}
Run Code Online (Sandbox Code Playgroud)
给我一个错误:
The iterator of this Seq is already in use/consumed by another Seq
(you might solve this by adding .cache on usages of the Seq, or
by assigning the Seq into an array)
in sub MAIN at ./pascal1 line 8
in block <unit> at ./pascal1 line 1
Run Code Online (Sandbox Code Playgroud)
知道如何解决吗?我试过.cache
在几个地方添加,但没有运气。
Z
返回一个 Seq
当 aSeq
产生它的下一个值时,它会将前一个值扔掉。
所以你通常只能从Seq
一次中获取值。
{…}
您拥有的块通过查看Seq
它生成的前一个块来工作。所以有一个问题。要么你看到里面有什么Seq
,要么...
操作员看到里面有什么Seq
问题是,您可能不希望 的结果Z
是Seq
,而是希望它是List
。
毕竟你...
用一个List
(1,)
.
((1,), { ((0, |@^a) Z+ (|@^a, 0)).List } ... *)
Run Code Online (Sandbox Code Playgroud)