Jonathan Scott Duff <[EMAIL PROTECTED]> writes:

> On Wed, Oct 30, 2002 at 04:03:55PM +0000, Piers Cawley wrote:
>> Jonathan Scott Duff <[EMAIL PROTECTED]> writes:
>> > Hey, that's neat. Although it looks like it returns the $src when there
>> > isn't a path. You probably want it to return undef or something.
>> 
>> Nah, it'll die when there isn't a path.
>
> duh!  Of course.  I was too busy thinking about the recursion.
>
>> > Perhaps where you have "die" there should be something like
>> > $src.collapse or maybe just "return undef".
>> 
>> Well, traditionally you have 'fail' there, wants to be some kind of
>> exception, but I'm not entirely sure of what the semantics should
>> be.
>
> In my very-non-exceptional programming, I'd want undef if there wasn't
> a path.   So, I guess your code is just fine but needs a CATCH block
> in there.  The superposition collapses when it finds a path or finds
> that there is no path, no backtracking would be required.

Tell you what, here's a non flexop translation of the Lisp code I
borrowed my example from. Assume for a moment that Perl has
call_with_current_continuation (it should have *something* like it...)

  my @paths;

  sub choose(*@choices) {
    fail unless @choices;
    call_with_current_continuation -> $cc {
      push @paths, sub { $cc( choose @choices[[EMAIL PROTECTED]] ) };
    }
  }

  sub fail {
    return "Exhausted Choices" but false unless @paths;
    my $p1 = pop @choices;
    $p1.();
  }

  sub descent($src, $dst) {
    when $src == $dst { return $dst }
    when !$src.kids   { fail }
    otherwise { return $src, descent(choose($src.kids), $dst) }
  }

Actually, doing things this way allows one to control whether one
searchs depth or breadth first. For breadth first, just change the
'push' in choose to an unshift...
  

-- 
Piers

   "It is a truth universally acknowledged that a language in
    possession of a rich syntax must be in need of a rewrite."
         -- Jane Austen?

Reply via email to