John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:
Here is the pseudocode expressed literally in Perl 6, using the modern (modulo) approach. Note that the subtraction approach might prove useful in the quest to make it shorter.
The rules are to assume the structure provided and just write code for the body, as marked between the comments. You can either put the result in $a or have it be the last value for implicit return.sub gcd ($a is copy, $b is copy -> Int) { # start of golf course while $b != 0 { my $t = $b; $b = $a % $b; $a = $t } # end of golf course option 1 return $a # end, option 2 }
The parameters are not typed, so you can use strange things (not just Ints) if you want during the process, and the result is automatically converted to Int because the return is typed.
I wonder what cool Perl 6 features can be brought to bear, or what tools from the Functional Programming arsinal can be wielded? Feel free to exhibit these for their own sake, even if not a short golf.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Euclidean algorithm Golf in Perl 6
by moritz (Cardinal) on Jun 18, 2009 at 16:28 UTC | |
by John M. Dlugosz (Monsignor) on Jun 19, 2009 at 14:30 UTC | |
|
Re: Euclidean algorithm Golf in Perl 6
by tilly (Archbishop) on Jun 18, 2009 at 17:42 UTC |