perl6将运算符作为参数传递

lis*_*tor 2 parameters arguments operators perl6

我试图通过定义和传递原始运算符来实现类似于Scheme的一些功能.在Scheme中,你可以

(define x +) (x 1 2)
Run Code Online (Sandbox Code Playgroud)

并且此操作将给出答案3.在perl6中,我不得不将操作符放在另一个函数中以实现相同的操作:

sub x($a) { $a * 2; }
sub y($m, $n) { $m($n); }
say y(&x, 3); # gives you 6
say y(+, 3) # this is error
Run Code Online (Sandbox Code Playgroud)

是否有一种简单的方法来传递运算符和函数?

谢谢.

Chr*_*oph 6

我不做Scheme,但是

(define x +)
Run Code Online (Sandbox Code Playgroud)

应该对应的东西

my constant &x = &infix:<+>;
Run Code Online (Sandbox Code Playgroud)

然后,

say x(1,2)
Run Code Online (Sandbox Code Playgroud)

也将输出3.

&[+]如果您发现上面使用的规范名称过于繁琐,那么也可以使用简写符号来传递运算符.

请注意,我不完全确定您尝试使用Perl6代码,因为它与Scheme代码段有很大不同.