在方案中,您可以使用for-each
在中迭代多个列表
> (for-each (lambda (a b) (display (+ a b)) (newline)) '(10 20 30) '(1 2 3))
11
22
33
>
我知道在Perl语言中,您可以使用for
遍历单个列表。像Scheme示例中那样迭代多个列表的好方法是什么?
我对Perl5或6的答案感兴趣。
发布于 2011-06-10 08:59:17
在Perl5中,您可以使用模块List::MoreUtils。或者成对使用,或者使用each_array返回的迭代器(可能需要两个以上的数组来并行迭代)。
use 5.12.0;
use List::MoreUtils qw(pairwise each_array);
my @one = qw(a b c d e);
my @two = qw(q w e r t);
my @three = pairwise {"$a:$b"} @one, @two;
say join(" ", @three);
my $it = each_array(@one, @two);
while (my @elems = $it->()) {
say "$elems[0] and $elems[1]";
}
发布于 2011-06-10 15:00:37
在Perl6中,Zip操作符是最好的选择。如果你想得到这两个值(而不是直接计算和),你可以不带加号使用它:
for (10, 11, 12) Z (1, 2, 3) -> $a, $b {
say "$a and $b";
}
发布于 2011-06-10 08:19:20
使用Zip操作符,您可以实现使用Scheme所做的操作:
> .say for (10, 20, 30) Z+ (1, 2, 3)
11
22
33
请参阅http://perlcabal.org/syn/S03.html#Zip_operators
https://stackoverflow.com/questions/6300674
复制相似问题