sub up { my ($row, $col) = split /-/, $_[0]; return $_[0] eq '0-0' ? "@_\n" : ($row * $col > 0 && up( ~-$row . '-' . ~-$col, @_ ) ) . ($row > $col && up( ~-$row . '-' . $col, @_ ) ); } #### sub up{ # indent to evidentiate the recursive function level # using the last index of the arguments array my $ind = ' ' x $#_ x 4; print $ind."\@_ = [@_]\n"; my ($row, $col) = split /-/, $_[0]; # if the first argument is 0-0 we are arrived to # the upper edge: time to return the result if ($_[0] eq '0-0'){ print $ind."RETURNING: \@_ is a valid path [@_]\n"; return "@_\n" } else{ # check A if ($row * $col > 0){ # show what happens if check A pass print $ind."$row * $col > 0 ". "&& up(",$row-1,'-',$col-1,", ",(join ', ',@_), ') # decremented both row and column are passed plus original @_ ',"\n"; } else{ print $ind."$row * $col > 0 is FALSE...\n"; } # execute the code if check A pass as shown above ($row * $col > 0 && up( ~-$row . '-' . ~-$col, @_ ) ) . ( # check B eval{ # show what happens if check B pass if ($row > $col){ print $ind."$row > $col ". "&& up(",$row-1,'-',$col,", ",(join ', ',@_), ') # decremented row and original column are passed plus original @_ ',"\n"; } else{print $ind."$row > $col is FALSE...\n"} # the eval block return empty string # to not pollute the output of the function ''; } ). # execute the code if check B pass as shown above ($row > $col && up( ~-$row . '-' . $col, @_ ) ); } } #### @_ = [3-1] 3 * 1 > 0 && up(2-0, 3-1) # decremented both row and column are passed plus original @_ @_ = [2-0 3-1] 2 * 0 > 0 is FALSE... 2 > 0 && up(1-0, 2-0, 3-1) # decremented row and original column are passed plus original @_ @_ = [1-0 2-0 3-1] 1 * 0 > 0 is FALSE... 1 > 0 && up(0-0, 1-0, 2-0, 3-1) # decremented row and original column are passed plus original @_ @_ = [0-0 1-0 2-0 3-1] RETURNING: @_ is a valid path [0-0 1-0 2-0 3-1] 3 > 1 && up(2-1, 3-1) # decremented row and original column are passed plus original @_ @_ = [2-1 3-1] 2 * 1 > 0 && up(1-0, 2-1, 3-1) # decremented both row and column are passed plus original @_ @_ = [1-0 2-1 3-1] 1 * 0 > 0 is FALSE... 1 > 0 && up(0-0, 1-0, 2-1, 3-1) # decremented row and original column are passed plus original @_ @_ = [0-0 1-0 2-1 3-1] RETURNING: @_ is a valid path [0-0 1-0 2-1 3-1] 2 > 1 && up(1-1, 2-1, 3-1) # decremented row and original column are passed plus original @_ @_ = [1-1 2-1 3-1] 1 * 1 > 0 && up(0-0, 1-1, 2-1, 3-1) # decremented both row and column are passed plus original @_ @_ = [0-0 1-1 2-1 3-1] RETURNING: @_ is a valid path [0-0 1-1 2-1 3-1] 1 > 1 is FALSE... "0-0 1-0 2-0 3-1\n0-0 1-0 2-1 3-1\n0-0 1-1 2-1 3-1\n" #### # receives and returns aoa sub up_modified{ my $ind = ' ' x $#{$_[0]} x 4; print $ind."\@_ is "; dd @_; my ($row, $col) = ($_[0][0][0],$_[0][0][1]); print $ind."receiving row $row col $col \n"; if ($row == 0 and $col == 0){ print $ind."RETURNING: "; dd @_; return @_; } else{ ( $row * $col > 0 && up_modified( [[~-$row, ~-$col],map {@$_}@_] ) ). ( $row > $col && up_modified( [[~-$row, $col], map {@$_}@_] ) ); } } #### @_ is [[3, 1]] receiving row 3 col 1 @_ is [[2, 0], [3, 1]] receiving row 2 col 0 @_ is [[1, 0], [2, 0], [3, 1]] receiving row 1 col 0 @_ is [[0, 0], [1, 0], [2, 0], [3, 1]] receiving row 0 col 0 RETURNING: [[0, 0], [1, 0], [2, 0], [3, 1]] @_ is [[2, 1], [3, 1]] receiving row 2 col 1 @_ is [[1, 0], [2, 1], [3, 1]] receiving row 1 col 0 @_ is [[0, 0], [1, 0], [2, 1], [3, 1]] receiving row 0 col 0 RETURNING: [[0, 0], [1, 0], [2, 1], [3, 1]] @_ is [[1, 1], [2, 1], [3, 1]] receiving row 1 col 1 @_ is [[0, 0], [1, 1], [2, 1], [3, 1]] receiving row 0 col 0 RETURNING: [[0, 0], [1, 1], [2, 1], [3, 1]] #### sub up_modified_bis{ my $ind = ' ' x $#_ x 4; print $ind."\@_ is "; dd @_; my ($row, $col) = ($_[0][0],$_[0][1]); print $ind."receiving row $row col $col \n"; if ($row == 0 and $col == 0){ print $ind."RETURNING: "; dd @_; return @_; } else{ ( $row * $col > 0 && up_modified_bis( [~-$row, ~-$col],map {[@$_]}@_ ) ). ( $row > $col && up_modified_bis( [~-$row, $col], map {[@$_]}@_ ) ); } } #output of called; up_modified_bis ([(3,1)]); @_ is [3, 1] receiving row 3 col 1 @_ is ([2, 0], [3, 1]) receiving row 2 col 0 @_ is ([1, 0], [2, 0], [3, 1]) receiving row 1 col 0 @_ is ([0, 0], [1, 0], [2, 0], [3, 1]) receiving row 0 col 0 RETURNING: ([0, 0], [1, 0], [2, 0], [3, 1]) @_ is ([2, 1], [3, 1]) receiving row 2 col 1 @_ is ([1, 0], [2, 1], [3, 1]) receiving row 1 col 0 @_ is ([0, 0], [1, 0], [2, 1], [3, 1]) receiving row 0 col 0 RETURNING: ([0, 0], [1, 0], [2, 1], [3, 1]) @_ is ([1, 1], [2, 1], [3, 1]) receiving row 1 col 1 @_ is ([0, 0], [1, 1], [2, 1], [3, 1]) receiving row 0 col 0 RETURNING: ([0, 0], [1, 1], [2, 1], [3, 1])