是否可以通过这种方式评估两个列表的积和?
是的。
让我们从链接开始:
有一个内置的sum,但没有内置的product,所以我们需要处理后者。一种方法是将[Z*] 写为reduction(这意味着它必须出现在它减少的列表的左侧)。另一种是写一个新的product函数。
综上所述,以下是我们可以编写“乘积总和”的一些方法:
say [+] [Z*] @a, @b; # 50 (closest to what it seems you want)
say sum [Z*] @a, @b; # 50 (`sum` is idiomatic)
say sum zip :with(&[*]), @a, @b; # 50 (some prefer `zip :with(&[*])` over `[Z*]`)
say sum product @a, @b; # 50 (requires a user defined `sub`)
sub product (|lists) { zip :with(&[*]), |lists }
按照上述方式进行操作意味着您可以使用任意数量的列表,而不仅仅是两个。
如果您真的想坚持使用中缀表示法,这会将您限制为两个列表,您可以对产品部分执行此操作:
say [+] @a Z* @b; # 50 (using infix sequential shallow zip metaop)
say [+] @a >>*<< @b; # 50 (using infix parallel nesting hyper metaop)
say sum @a Z* @b; # 50 (replacing `sum` with `[+]` reduction)
say sum @a >>*<< @b; # 50
不同的链式变体发生了什么导致6、(10)、(30) 和125 等结果?
Raku 正确地执行了您的代码所表达的内容。
以下代码示例解释了@Zaid 共享的所有代码/结果。你(任何读者,不仅仅是@Zaid)可能需要做一些工作来理解为什么;如果您无法弄清楚这些示例如何解释 Zaid 的一个或多个结果,请发表评论,我很乐意在评论中解释和/或更新此答案,并感谢您的问题/评论。
say my @a = 2...6; # [2 3 4 5 6]
say my @b = 5...1; # [5 4 3 2 1]
say [+] [5,4,3,2,1]; # 15
# Same as:
say sum @b; # 15
say 2 Z* 15; # (30)
# zip stops once shortest list exhausted, so same as:
say [2,3,4,5,6] Z* 15; # (30)
say +@b; # 5
# `+` coerces argument(s) to number, so short for:
say elems @b; # 5
# Same as:
say elems [5,4,3,2,1]; # 5
say 2 Z* 5; # (10)
#say +foo; # Error while compiling: Undeclared ... foo
# Same effect as:
#say foo; # Error while compiling: Undeclared ... foo
say [Z*] @b; # (120)
# Same as:
say 5 Z* 4 Z* 3 Z* 2 Z* 1; # (120)
say @a [Z*] 5; # (10)
# square brackets redundant, and
# zip stops once shortest list exhausted, so same as:
say 2 Z* 5; # (10)
say [+] @a >>*<< @b; # 50
say [>>*<<] @b; # 120
# hypers redundant if both args scalars, so same as:
say [*] [5,4,3,2,1]; # 120
# (5 * 4 * 3 * 2 * 1)
say @a [+] [>>*<<] @b; # 125
# square brackets round infix `+` redundant, so same as:
say @a + [>>*<<] @b; # 125
# hypers redundant if both args scalars, so same as:
say 5 + [*] @b; # 125 (5 + 120)
say @a [>>*<<][+] @b; # Same as:
say @a [>>*<<] [+] @b; #
say @a [>>*<<] sum @b; #
say @a >>*<< sum @b; #
#say @a >>*<< 15; # Lists on either side ... not of the same length