如何控制 Perl 6 中循环的嵌套?

发布于 2024-10-09 01:19:34 字数 221 浏览 0 评论 0原文

这个程序应该编写总和小于或等于 7 的索引三元组:

for ((1..7) X (1..7)) X (1..7) {
 .say if [+] $_ <= 7;
}

我认为它只会在列表的顶层循环(然后代码会在循环体中出现错误,但这不是点),但它只是循环遍历单个数字,这令人沮丧:( 有一个巧妙的技巧来避免它吗?顺便说一句,有没有办法制作 n 元直接乘积?

This program should've written triples of indices that have a sum less or equal to 7:

for ((1..7) X (1..7)) X (1..7) {
 .say if [+] $_ <= 7;
}

I thought it would only loop over the top level of the list (and the code would have an error in the loop body then, but it's not the point), but it just loops over individual numbers, which is frustrating :( Is there a neat trick to avoid it? And BTW, is there a way to make an n-ary direct product?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你爱我像她 2024-10-16 01:19:34

命名参考的最简单方法

for (1..7) X (1..7) -> $a, $b { }

the easiest way to to name the reference

for (1..7) X (1..7) -> $a, $b { }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文