Anonymous user
Ethiopian multiplication: Difference between revisions
→{{header|Perl 6}}: modernize
(Updated D entry) |
(→{{header|Perl 6}}: modernize) |
||
Line 2,388:
=={{header|Perl 6}}==
<lang perl6>sub halve (Int $n is rw) { $n div= 2 }
sub double (Int $n is rw) { $n *= 2 }
sub even (Int $n --> Bool) { $n %% 2 }
sub
my Int $r = 0;
while $a {
Line 2,402 ⟶ 2,400:
}
return $r;
}
say ethiopic-mult(17,34);</lang>
{{out}}
<pre>578</pre>
More succinctly using implicit typing, primed lambdas, and an infinite loop:
<lang perl6>sub
my &halve = * div= 2;
my &double = * *= 2;
Line 2,416 ⟶ 2,419:
double $b;
}
}
say ethiopic-mult(17,34);</lang>
More succinctly still, using a pure functional approach (reductions, mappings, lazy infinite sequences):
<lang perl6>sub halve { $^n div 2 }
Line 2,422 ⟶ 2,427:
sub even { $^n %% 2 }
sub
[+] ($b, &double ... *)
Z*
}
say ethiopic-mult(17,34);</lang>
(same output)
=={{header|PHP}}==
|