Last active
August 29, 2015 14:06
-
-
Save marcoonroad/07ca9726708d8bb07da5 to your computer and use it in GitHub Desktop.
Only a experiment on Perl version 5.20...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/home/usuario/localperl/bin/perl | |
# multidispatch functions | |
use v5.20; | |
use feature qw/ say signatures /; | |
use strict; | |
use warnings; | |
#------------------------------------------------# | |
# # | |
# Mutual Recursion with MultiDispatch-like Table # | |
# # | |
#------------------------------------------------# | |
# F (0) = 1; M (0) = 0 # | |
# # | |
# F (n) = n - M (F (n - 1)), n > 0 # | |
# M (n) = n - F (M (n - 1)), n > 0 # | |
# # | |
#------------------------------------------------# | |
sub dispatcher ($args, $multi) { | |
my %dispatch; | |
%dispatch = ( | |
0 => sub ( ) { $multi -> [0] -> ( ) }, | |
n => sub ( ) { $multi -> [1] -> ($args -> [0]) }, | |
); | |
($dispatch{ $args -> [0] } || $dispatch{ n }) -> ( ); | |
} | |
my (@F, @M); | |
@F = ( | |
sub ( ) { 1 }, | |
sub ($n) { $n - dispatcher ([ dispatcher ([$n - 1], \@F) ], \@M) } | |
); | |
@M = ( | |
sub ( ) { 0 }, | |
sub ($n) { $n - dispatcher ([ dispatcher ([$n - 1], \@M) ], \@F) } | |
); | |
foreach my $ref (\@F, \@M) { | |
my $result = [ map { dispatcher ([$_], $ref) } 0 .. 19 ]; | |
say "@{ $result }"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment