Started this before LanX' answer (trying to implement whatever I understood reading the HOP):

use strict; use warnings; use 5.010; # defined-or use Data::Dump 'dd'; sub get_iterator { my ( $x_to, $y_to, $x_from, $y_from ) = @_; $x_from //= 0; $y_from //= 0; # store partial solutions to explore, to follow left or right # branches, respectively my @left_agenda = [[ $x_from, $y_from ]]; my @right_agenda = [[ $x_from, $y_from ]]; return sub { LOOP: { return undef unless @left_agenda || @right_agenda; my ( $ref, $right ); unless ( $ref = pop @left_agenda ) { $ref = pop @right_agenda; $right = 1 } my @path = @$ref; my ( $x, $y ) = @{ $path[ -1 ] }; $x ++; $y ++ if $right; if ( $x <= $x_to and $y <= $y_to ) { push @path, [ $x, $y ]; return \@path if $x == $x_to and $y == $y_to; push @left_agenda, \@path; push @right_agenda, \@path; } redo LOOP; } } } my $iter = get_iterator( 5, 2 ); my $solution; dd $solution while $solution = $iter-> ();

Update. Slightly refactored version, eliminating "left agenda", i.e. pushing and immediately popping. Still, it's ugly because from initial point we can descend left and right, but from any partial solution from agenda - right only, left direction already exhausted. Hence, the "init" flag.

use strict; use warnings; use 5.010; # defined-or use Data::Dump 'dd'; sub get_iterator { my ( $x_to, $y_to, $x_from, $y_from ) = @_; $x_from //= 0; $y_from //= 0; my @agenda = [[ $x_from, $y_from ]]; my $init = 1; return sub { my @path; ( @path, $init ) = @{ $agenda[ -1 ]} if $init; LOOP: { return undef unless @path || @agenda; my $right; unless ( @path ) { @path = @{ pop @agenda }; $right = 1 } my ( $x, $y ) = @{ $path[ -1 ] }; $x ++; $y ++ if $right; if ( $x <= $x_to and $y <= $y_to ) { push @path, [ $x, $y ]; return \@path if $x == $x_to and $y == $y_to; push @agenda, [ @path ] } else { @path = (); } redo LOOP; } } } my $iter = get_iterator( 5, 2 ); my $solution; dd $solution while $solution = $iter-> ();

In reply to Re: compute paths in Pascal's triangle (aka Tartaglia's one) by vr
in thread compute paths in Pascal's triangle (aka Tartaglia's one) by Discipulus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.