LoginSignup
2
1

More than 5 years have passed since last update.

冪集合

Posted at
powerset.pl
  4 my @a = (1,2,3);
  5 
  6 my @p = ([]);
  7 for my $a(@a) {
  8     @p = (@p, map {[@$_, $a]} @p);
  9 }   

これ以上簡単な書き方がわからなかった。
Haskellだと『すごいHaskell』にもある通りで

powerset.hs
  1 import Control.Monad
  2 powerset :: [a]->[[a]]
  3 powerset xs = filterM (\x->[True,False]) xs

*Main> :l powerset.hs 
[1 of 1] Compiling Main             ( powerset.hs, interpreted )
Ok, modules loaded: Main.

*Main> powerset [1..3]
[[1,2,3],[1,2],[1,3],[1],[2,3],[2],[3],[]]
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1