[Question]
  [
## Problem definition
Print out the powerset of a given set. For example:
```
[1, 2, 3] => [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]
```
Each element is to be printed on a separate line, so the above example would be printed as:
```
[]
[1]
[2]
...
[1, 2, 3]
```
## Example code (in D, python example [here](https://stackoverflow.com/questions/1482308/whats-a-good-way-to-combinate-through-a-set)):
```
import std.stdio;
string[][] powerset(string[] set) {
    if (set.length == 1) {
        return [set, []];
    }
    string[][] ret;
    foreach (item; powerset(set[1 .. $])) {
        ret ~= set[0]~item;
        ret ~= item;
    }
    return ret;
}
void main(string[] argv) {
    foreach (set; powerset(argv[1 .. $]))
        writeln(set);
}
```
## Input
Elements will be passed as arguments. For example, the example provided above would be passed to a program called `powerset` as:
```
powerset 1 2 3
```
Arguments will be alphanumeric.
## Rules
1. No libraries besides io
2. Output does not have to be ordered
3. Powerset does not have to be stored, only printed
4. Elements in the set must be delimited (e.g. `1,2,3`, `[1,2,3]` and `['1','2','3']` are acceptable, but `123` is not
	* Trailing delimiters are fine (e.g. `1,2,3, == 1,2,3`)
5. *Best* is determined based on number of bytes
The best solution will be decided no less than 10 days after the first submission.
  
[Answer]
# Mathematica 16
**Code** 
`Subsets` is native to Mathematica.
```
Column@Subsets@s
```
The code (without column) can be [verified on WolframAlpha](http://www.wolframalpha.com/input/?i=Subsets%5B%7B1,2,3%7D%5D). (I had to use brackets instead of `@`; they mean the same thing.
**Usage**
```
s={1,2,3}
Column@Subsets@s
```
![output](https://i.stack.imgur.com/zez7U.png)
---
This method (55 chars) uses the approach suggested by @w0lf.
```
s #&/@Tuples[{0,1},Length@s]/.{0:>Sequence[]}//Column
```
**Breakdown**
Generate the tuples, composed of `0` and `1`'s of length `Length[s]`
```
Tuples[{0, 1}, Length@s]
```
> 
> {{0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1,
>  1, 0}, {1, 1, 1}}
> 
> 
> 
Multiply the original list (vector) by each tuple:
```
s # & /@ Tuples[{0, 1}, Length@s]
```
> 
> {{0, 0, 0}, {0, 0, 3}, {0, 2, 0}, {0, 2, 3}, {1, 0, 0}, {1, 0, 3}, {1,
>  2, 0}, {1, 2, 3}}
> 
> 
> 
Delete the `0`'s. `%` is shorthand for "the preceding output".
%/. {0 :> Sequence[]}
> 
> {{}, {3}, {2}, {2, 3}, {1}, {1, 3}, {1, 2}, {1, 2, 3}}
> 
> 
> 
Display in column:
![Mathematica graphics](https://i.stack.imgur.com/MKYu1.png)
[Answer]
## C, 118 115
Whilst can save approx 20 chars with simpler formatting, still not going to win in code golf terms either way.
```
x,i,f;
main(int a,char**s){
    for(;x<1<<a;x+=2,puts("[]"+f))
        for(i=f=0;++i<a;)x&1<<i?f=!!printf("%c%s","[,"[f],s[i]):0;
}
```
Testing:
```
/a.out 1 2 3
[]
[1]
[2]
[1,2]
[3]
[1,3]
[2,3]
[1,2,3]
```
[Answer]
### GolfScript, 22 18 characters
```
~[[]]{{+}+1$%+}@/`
```
Another attempt in GolfScript with a completely different algorithm. Input format is the same as with w0lf's answer. ([online test](http://golfscript.apphb.com/?c=OydbMSAyIDNdJwoKfltbXV17eyt9KzEkJSt9QC9g&run=true))
[Answer]
## GolfScript (43 chars)
This may seem quite long, but it's the first solution to follow the spec: input is from command-line arguments, and output is newline-delimited.
```
"#{ARGV.join('
')}"n/[[]]\1/{`{1$+.p}+%}%p;
```
E.g.
```
$ golfscript.rb powset.gs 1 2 3
["1"]
["2"]
["2" "1"]
["3"]
["3" "2"]
["3" "1"]
["3" "2" "1"]
[]
```
[Answer]
## awk (82)
```
{for(;i<2^NF;i++){for(j=0;j<NF;j++)if(and(i,(2^j)))printf "%s ",$(j+1);print ""}}
```
assume saved in file powerset.awk, usage
```
$ echo 1 2 3 | awk -f powerset.awk
1
2
1 2
3
1 3
2 3
1 2 3
```
**ps** if your awk doesn't have and() function, replace it with `int(i/(2^j))%2` but adds two to the count.
[Answer]
## JavaScript, 98
Sadly, a good chunk is spent on output formatting.
```
for(n in a=eval(prompt(i=p=[[]])))
    for(j=i+1;j;)
        p[++i]=p[--j].concat(a[n]);
alert('[]'+p.join('\n'))
```
### Input
Takes a JavaScript array. (e.g. [1,2,3])
### Output
```
[]
1
1,2
2
2,3
1,2,3
1,3
3
```
[Answer]
## J, 19 chars
```
   (<@#~#:@i.@(2&^)@#)
   (<@#~#:@i.@(2&^)@#) 1 2 3
┌┬─┬─┬───┬─┬───┬───┬─────┐
││3│2│2 3│1│1 3│1 2│1 2 3│
└┴─┴─┴───┴─┴───┴───┴─────┘
```
The ascii boxing in the output is called `boxing` and provides heterogen collection (for different length of arrays here).
[Answer]
## Python ~~70~~ 67 bytes
```
def p(a,*v):
 i=0;print v
 for n in a:i+=1;p(a[i:],n,*v)
p(input())
```
Input is taken in the same manner as for [ugoren's solution](https://codegolf.stackexchange.com/questions/9045/shortest-power-set-implementation/9060#9060). Sample I/O:
```
$ echo [1,2,3] | powerset.py
()
(1,)
(2, 1)
(3, 2, 1)
(3, 1)
(2,)
(3, 2)
(3,)
```
[Try it online!](https://tio.run/##FcoxDoAgEATAnldsCUoh2GF4iaEgUeI1x8Wgia9HnXrkaUdl3/u2F4jOdrhNUKA4LXISN9wKpZ5gECMHGqNbvrZSSJb/rEQTy9W0Mb2vzsJbzOkF "Python 2 – Try It Online")
[Answer]
## Golfscript 48
```
~:x,:§2\?,{[2base.,§\-[0]*\+x\]zip{~{}{;}if}%p}%
```
This program uses the binary representations of numbers from 0 to length(input) to generate powerset items.
### Input
The input format is the Golfscript array format (example: `[1 2 3]`)
### Output
The output is a collection of arrays separated by newlines, representing the power set. Example:
```
[]
[3]
[2]
[2 3]
[1]
[1 3]
[1 2]
[1 2 3]
```
### Online Test
The program can be tested online [here](http://golfscript.apphb.com/?c=OydbMSAyIDNdJwoKfjp4LDrCpzJcPyx7WzJiYXNlLizCp1wtWzBdKlwreFxdemlwe357fXs7fWlmfSVwfSU%3D&run=true).
[Answer]
# Python (74 70 chars)
```
def p(a,v):
 if a:i,*a=a;p(a,v);p(a,v+[i])
 else:print v
p(input(),[])
```
for input as `1,2,3` or `[1,2,3]`, output is:
```
[]
[3]
[2]
[2, 3]
[1]
[1, 3]
[1, 2]
[1, 2, 3]
```
[Answer]
## Haskell (96)
```
import Control.Monad
import System.Environment
main=getArgs>>=mapM print.filterM(\_->[False ..])
```
If importing `Control.Monad` isn't allowed, this becomes **100** characters:
```
import System.Environment
main=getArgs>>=mapM print.p
p z=case z of{[]->[[]];x:y->p y++map(x:)(p y)}
```
[Answer]
## Ruby, 39
```
$*.map{p *$*.combination($.)
$.+=1}
p$*
```
[Answer]
**Mathematica 53**
```
Column@Fold[#~Join~Table[x~Join~{#2},{x,#}]&,{{}},#]&
```
![enter image description here](https://i.stack.imgur.com/nb8Lu.png)
[Answer]
## APL (26)
Reads input from keyboard because there's no `argv` equivalent.
```
↑⍕¨(/∘T)¨↓⍉(M/2)⊤⍳2*M←⍴T←⎕
```
Usage:
```
      ↑⍕¨(/∘T)¨↓⍉(M/2)⊤⍳2*M←⍴T←⎕
⎕:
      1 2 3
3    
2    
2 3  
1    
1 3  
1 2  
1 2 3
```
Explanation:
* `T←⎕`: read input, store in `T`
* `M←⍴T`: store length of `T` in `M`
* `(M/2)⊤⍳2*M`: generate the bit patterns for `1` upto `2^M` using `M` bits.
* `↓⍉`: split the matrix so that each bit pattern is separate
* `(/∘T)¨`: for each bit pattern, select those sub-items from `T`.
* `↑⍕¨`: for output, get the string representation of each element (so that it will fill using blanks and not zeroes), and format as a matrix (so that each element is on its own line).
[Answer]
**Scala, 81**
```
def p[A](x:Seq[A]){x.foldLeft(Seq(Seq[A]()))((a,b)=>a++a.map(b+:_)).map(println)}
```
[Answer]
# JavaScript (*ES6*) 76
Partially copied from this one: <https://codegolf.stackexchange.com/a/51502/21348>
Using a bitmap, so it's limited to no more than 32 elements.
Run the snippet in Firefox to test.
```
f=l=>{ 
  for(i=0;i<1<<l.length;i++)
    console.log(l.filter(v=>[i&m,m+=m][0],m=1))
}  
// TEST
// Redefine console to have output inside the page
console = { log: (...p) => O.innerHTML += p.join(' ') + '\n' }
test=()=>{
  var set = I.value.match(/[^ ,]+/g)
  O.innerHTML='';
  f(set);
}
test()
```
```
#I,#O { border: 1px solid #aaa; width: 400px; padding:2px}
```
```
Insert values, space or comma separated:<br>
<input id=I value='1 2 3'> <button onclick="test()">-></button>
<pre id=O></pre>
```
[Answer]
# C# 164
Man this is hard in C#!
```
void P<T>(T[]c){foreach(var d in c.Aggregate<T,IEnumerable<IEnumerable<T>>>(new[]{new T[0]},(a,b)=>a.Concat(a.Select(x=>x.Concat(new[]{b})))))Console.WriteLine(d);}
```
[Answer]
# K, 14 bytes
```
{x@&:'!(#x)#2}
```
Generate all 0/1 vectors as long as the input, gather the indices of 1s and use those to select elements from the input vector. In practice:
```
  {x@&:'!(#x)#2} 1 2 3
(!0
 ,3
 ,2
 2 3
 ,1
 1 3
 1 2
 1 2 3)
```
This is a bit liberal with the output requirements, but I think it's legal. The most questionable part is that the empty set will be represented in a type dependent form; `!0` is how K denotes an empty numeric vector:
```
  0#1 2 3      / integers
!0
  0#`a `b `c   / symbols
0#`
  0#"foobar"   / characters
""
```
## Explanation
The `(#x)#2` builds a vector of `2` as long as the input:
```
  {(#x)#2}1 2 3
2 2 2
  {(#x)#2}`k `d `b `"+"
2 2 2 2
```
When monadic `!` is applied to a vector, it is "odometer":
```
  !2 2 2
(0 0 0
 0 0 1
 0 1 0
 0 1 1
 1 0 0
 1 0 1
 1 1 0
 1 1 1)
```
Then we use "where" (`&`) on each (`'`) vector to gather its indices. The colon is necessary to disambiguate between the monadic and dyadic form of `&`:
```
  &0 0 1 0 1 1
2 4 5
  {&:'!(#x)#2} 1 2 3
(!0
 ,2
 ,1
 1 2
 ,0
 0 2
 0 1
 0 1 2)
```
If we just wanted combination vectors, we'd be done, but we need to use these as indices into the original set. Fortunately, K's indexing operator `@` can accept a complex structure of indices and will produce a result with the same shape:
```
  {x@&:'!(#x)#2} `a `c `e
(0#`
 ,`e
 ,`c
 `c `e
 ,`a
 `a `e
 `a `c
 `a `c `e)
```
Elegant, no?
[Answer]
## Python 2, 64 bytes
Using comma-separated input:
```
P=[[]]
for i in input():P+=[s+[i]for s in P]
for s in P:print s
```
## Pyth, 4 bytes (using builtin) or 14 bytes (without)
As noted by @Jakube in the comments, Pyth is too recent for this question. Still here's a solution using Pyth's builtin powerset operator:
```
jbyQ
```
And here's one without it:
```
jbu+Gm+d]HGQ]Y
```
You can try both solutions [here](https://pyth.herokuapp.com/?code=jbyQ&input=1,+2,+3&debug=0) and [here](https://pyth.herokuapp.com/?code=jbu%2BGm%2Bd%5DHGQ%5DY&input=1,+2,+3&debug=0). Here's an explanation of the second solution:
```
jb       # "\n".join(
 u       #  reduce(
  +G     #   lambda G,H: G+
   m     #    map(
    +d]H #     lambda d: d+[H],
    G    #     G),
  Q      #   input()
  ]Y     #   [[]]))
```
[Answer]
## brainfuck, 94 bytes
```
+[[<+>>+<-]++[>-<------]>-[>]<<[>>+>]>,]++++++++++[[[<]<]+[-[>[.>]]<[<]>+[>]>]<<
.[<<[<]>-]++>]
```
Formatted:
```
+
[
  [<+> >+<-]
  ++[>-<------]>-[>]
  <<[>>+>]
  >,
]
++++++++++
[
  [[<]<]
  +
  print
  [
    -[>[.>]]
    <[<]
    >+[>]
    >
  ]
  <<.
  increment
  [
    <<[<]
    >-
  ]
  ++>
]
```
Expects input of the form `9,10,11` without a trailing newline, and outputs subsets in the same format, sometimes with a trailing comma. The first line printed will always be empty, signifying the empty set.
[Try it online.](http://brainfuck.tryitonline.net/#code=K1tbPCs-Pis8LV0rK1s-LTwtLS0tLS1dPi1bPl08PFs-Pis-XT4sXSsrKysrKysrKytbW1s8XTxdK1stWz5bLj5dXTxbPF0-K1s-XT5dPDwuWzw8WzxdPi1dKys-XQ&input=MSwyMyw0LDU)
The basic idea is to place a bit next to each element, then repeatedly increment the binary number while printing the corresponding subset before each increment. (A bit indicates whether an element is in the subset.) A sentinel bit to the left of the array is used to terminate the program. This version actually creates an exponential number of sentinels to save some bytes; a more efficient 99-byte solution that only uses one sentinel can be found in the revision history.
Each bit is encoded as one plus its value; i.e., it can be either `1` or `2`. The tape is laid out with the bit before each element and a single zero cell between adjacent elements. The comma is included on the tape for non-final elements, so we can conveniently just print elements without doing any extra work to handle delimiters.
[Answer]
# [APL (Dyalog Classic)](https://www.dyalog.com/), 13 bytes
```
⍪,⊃∘.,/⎕,¨⊂⊂⍬
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v@ob2pm/qO2CQZcjzra0/4/6l2l86ir@VHHDD0dfaCczqEVj7qaQKh3zX@giv9pXIYKRgrGAA "APL (Dyalog Classic) – Try It Online")
Output:
```
 1 2 3 
 1 2   
 1 3   
 1     
 2 3   
 2     
 3     
```
There's a blank line at the end to represent the empty set.
Explanation:
`⎕` evaluated input
`⎕,¨⊂⊂⍬` append an empty numeric list after each element
`∘.,` Cartesian product
`/` reduction (foldr)
`⊃` disclose (necessary after reduction in APL)
At this point the result is an n-dimensional 2-by-...-by-2 array, where n is the length of the input.
`,` flatten into a vector
`⍪` turn the vector into an upright 2n-by-1 matrix, so each subset is on a separate line
[Answer]
# [Haskell](https://www.haskell.org/), ~~80~~ 78 bytes
```
import System.Environment
main=getArgs>>=mapM(print.concat).mapM(\a->[[a],[]])
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/PzO3IL@oRCG4srgkNVfPNa8ssyg/Lzc1r4QrNzEzzzY9tcSxKL3Yzs42N7HAV6OgKDOvRC85Py85sURTDywUk6hrFx2dGKsTHRur@f//f5P/xv@N/hsCAA "Haskell – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 6 bytes
```
ŒPŒṘ€Y
```
[Try it online!](https://tio.run/nexus/jelly#@390UsDRSQ93znjUtCby////hjpGOsYA "Jelly – TIO Nexus")
`ŒṘ€Y` are string formatting.
[Answer]
# [Python 2](https://docs.python.org/2/), 63 bytes
```
def f(s,p=[[]]):
 for e in s:
  for u in p:p=p+[u+[e]]
 print p
```
[Try it online!](https://tio.run/##HccrDsAgDABQv1NUQsCMORJO0tSNZhho@Iidvvs89@SeV6tB9cwMbIaXhEhk4wbcOmQoFcabf@ubREnicDnMRBtIL3WCKBvcffAHWX0A "Python 2 – Try It Online")
[Answer]
# JavaScript (ES6), 68 bytes
```
a=>alert(a.reduce((a,x)=>[...a,...a.map(y=>[...y,x])],[[]]).join`
`)
```
### Demo
```
let f =
a=>alert(a.reduce((a,x)=>[...a,...a.map(y=>[...y,x])],[[]]).join`
`)
f([1,2,3])
```
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 4 bytes
```
⊇ᵘẉᵐ
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/r//1FX@8OtMx7u6ny4dcL//9GGOkY6xrEA "Brachylog – Try It Online")
```
  ẉ     Write on its own line
 ᵘ ᵐ    every unique
⊇       subset of the input.
```
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 1 byte
```
y
```
Since Pyth has implicit `Q` (input variable) at the end of programs, this is basically just power set of the input. I don't think this violates any rules (although 'No libraries besides io' is a bit vague)
[Try it online!](https://tio.run/##K6gsyfj/v/L//2hDHQUjHQXjWAA "Pyth – Try It Online")
[Answer]
# [Japt](https://github.com/ETHproductions/japt) `-R`, ~~5~~ 1 [byte](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
~~Sadly, Japt's built-in for getting the powerset of an array doesn't include the empty array or this would be 1 byte.~~ It does now!
```
à
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LVI&code=4A&input=WzEsIDIsIDNd) (the empty line at the end is the empty set or you can [run it with the `-Q` flag](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LVE&code=4A&input=WzEsIDIsIDNd) instead to visualise the sub-arrays)
[Answer]
# [Zsh](https://www.zsh.org/), 31 bytes
Accidentally cut my previous byte-count in half answering another prompt...
```
for i;a=({$i,}\ $^a)
<<<${(F)a}
```
[Try it online!](https://tio.run/##qyrO@J@moanxPy2/SCHTOtFWo1olU6c2RkElLlGTy8bGRqVaw00zsfZ/QVFmXomCuq2trbomF1eagqGCEYRUMFYwAbISFZIUkv8DAA "Zsh – Try It Online")
Delimiter is one or more spaces, we abuse trailing delimiters extensively here.
[Answer]
# [Thunno 2](https://github.com/Thunno/Thunno2), 3 [bytes](https://github.com/Thunno/Thunno2/blob/main/docs/codepage.md)
```
ʠ¶j
```
[Try it online!](https://Not-Thonnu.github.io/run#aGVhZGVyPSZjb2RlPSVDQSVBMCVDMiVCNmomZm9vdGVyPSZpbnB1dD0xJTJDJTIwMiUyQyUyMDMmZmxhZ3M9)
#### Explanation
```
ʠ¶j  # Implicit input
ʠ    # Take the powerset
 ¶j  # Join on newlines
     # Implicit output
```
]