Reputation: 5749
I am trying to achieve some functionality similar to Scheme by defining and passing raw operators. In Scheme, you can
(define x +) (x 1 2)
and this operation will give you answer 3. In perl6, I somehow have to place the operator in another function in order to achieve the same:
sub x($a) { $a * 2; }
sub y($m, $n) { $m($n); }
say y(&x, 3); # gives you 6
say y(+, 3) # this is error
Is there an easy way to pass operators and functions?
Thanks.
Upvotes: 2
Views: 145
Reputation: 169563
I don't do Scheme, but
(define x +)
should correspond to something like
my constant &x = &infix:<+>;
Then,
say x(1,2)
will output 3 as well.
There's also a shorthand notation &[+]
for passing operators around if you find the canonical name used above too cumbersome.
Note that I'm not entirely sure what you were trying to do with your Perl6 code, as that looks rather different from the Scheme snippet.
Upvotes: 6