powerset operation

From: matt (mhm26_at_drexel.edu)
Date: 01/17/04

  • Next message: Erik Tank: "Re: multiple row updates in MYSQL using HTML"
    Date: 16 Jan 2004 22:50:38 -0800
    
    

    Hi,

    I would like to know how to implement a powerset operation in perl.
    My friend found the code:

    sub subsets {
      map {
        my $n = $_;
        [@_[grep {$n & 1 << $_} 0..$#_]]
      } (1..2**($#_ + 1));
    }

    online, and that makes sense - but I would like to understand how to
    build a lists of lists etc through the `normal' way of building the
    powerset.

    Normal being akin to the haskell code:
      powerset :: [a] -> [[a]]
      powerset [] = [[]]
      powerset (x:xs) = concat $ [[l, x:l] | l <- powerset xs]

    my closest perl approximation is:

    sub subsets {
      my @list;
      @list = @_;

      return (()) if ($#list < 0);

      my @subs;
      @subs = subsets(@subs[1..$#subs]);

      my @with;
      @with = @subs;
      foreach (@with) {
        push(@{$_}, $list[0]);
        push(@subs, [ $_ ]);
      }

      return @subs;
    }

    but this doesn't seem to work. (I'm relatively new to perl, but am a
    somewhat well-versed unix and ruby user, if where I'm coming from
    matters in explanations...)


  • Next message: Erik Tank: "Re: multiple row updates in MYSQL using HTML"

    Relevant Pages

    • Reg. Directory listing program
      ... I am new to this mailing list and I am very new to PERL. ... I wrote a code that lists files in a directory with the permissions. ... Directory listing program ...
      (perl.beginners)
    • Re: Can this be done (by a noob :))
      ... Perl has "modules" for handling specific tasks. ... The script would read the list and set the first pid in list as current ... Next is the new sid list created in the previous step, ... always excistant) lists. ...
      (comp.lang.perl.misc)
    • Re: Question on a perlsub statement.
      ... to send an eof so perl know it finished reading ALL the lines. ... assignment has all to do with it. ... perl6 will be able to do this due to lazy lists and ...
      (comp.lang.perl.misc)
    • Re: __DATA__ and in Forth
      ... I did not realise that perl was working one line at a time, ... compile-time; embedding a file is comparatively primitive. ... Maybe for lists of data that you want to embed in the compiled ...
      (comp.lang.forth)
    • Emulating Generators: Iterators for Lists (Newbie)
      ... achieving various things in Perl. ... What would be a good way to do an iterator for a sequence of values to be ... sub-routine, and declare the state you need to maintain between sub calls ... lists which might be the answer. ...
      (comp.lang.perl.misc)