Skip to content

Instantly share code, notes, and snippets.

@marcoonroad
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcoonroad/07ca9726708d8bb07da5 to your computer and use it in GitHub Desktop.
Save marcoonroad/07ca9726708d8bb07da5 to your computer and use it in GitHub Desktop.
Only a experiment on Perl version 5.20...
#!/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