Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Please help me understand this permutation sub?

by BillKSmith (Prior)
on Dec 09, 2020 at 04:30 UTC ( #11124863=note: print w/replies, xml ) Need Help??

in reply to Please help me understand this permutation sub?

Your sub is confusing. Note that the return statement is never executed. (You can replace it with die.) The code requires you to know exactly what happens when @set becomes empty. Are you sure you know the values of the special variable $#set, the range operator 0..$#set, and how foreach handles that? The code below avoids these issues, but is equivalent. At first, my logic appears more complicated, but I think you will find it easier to follow. Add the same debugging if you wish. Once you see what is going on, you should be able to see how the terse original is really the same thing.
use strict; use warnings; sub permutation { my ($perm,@set) = @_; unless(@set) { print "$perm\n" || die("Print error:$!"); } else { permutation($perm.$set[$_], (@set[0..$_-1],@set[$_+1..$#set]) ) foreach (0..$#set); } return; } my @input = (qw/a b c d/); permutation('',@input);

UPDATE: I now believe that you original code had a serious error and it was only by pure luck that it worked at all. The '||' operator before the 'return' should be an 'and' operator. With this change, function appears to work exactly the same. The difference is that now, the 'return' is executed immediately after every print. The behavior of 'foreach' in this special case is no longer an issue. Note that the lower priority operator 'and' instead of '&&' is needed to make the string bind to the print rather than the '&&'. The reverse logic is needed because print returns a true value on success. I have never seen a failure.


Replies are listed 'Best First'.
Re^2: Please help me understand this permutation sub?
by mdunnbass (Monk) on Dec 09, 2020 at 14:09 UTC

    Hi Bill,

    Thanks for the clarification in your code. I was clear on $#set and 0..$#set, but I got lost in the nested recursions. They went one level deeper than my brain was keeping track of. LanX did a great job of clearing up my confusion, above.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11124863]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2021-04-11 18:29 GMT
Find Nodes?
    Voting Booth?

    No recent polls found