Skip to content

Instantly share code, notes, and snippets.

@marcoonroad
Created October 12, 2014 04:35
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/1b7cabbaec3755a1c7fd to your computer and use it in GitHub Desktop.
Save marcoonroad/1b7cabbaec3755a1c7fd to your computer and use it in GitHub Desktop.
Um simples teste de funções com múltiplo despacho e recursão...
#!/home/user/localperl/bin/perl
use v5.20;
use strict;
use warnings;
use feature qw| say signatures |;
my %multi;
sub def ($name, $param, $lambda) {
$multi{ $name } -> { $param } = \&{ $lambda }
}
sub call ($name, $arg) {
$multi{ $name } -> { $arg } &&
$multi{ $name } -> { $arg } -> ( ) ||
$multi{ $name } -> { N } -> ($arg)
}
def fact => 2 => sub { 2 };
def fact => N => sub ($N) { $N * call fact => $N - 1 };
def fib => 1 => sub { 1 };
def fib => 2 => sub { 1 };
def fib => N => sub ($N) { (call fib => $N - 1) + (call fib => $N - 2) };
say call fact => 5;
say call fib => 10;
# end of script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment