The power element of abcd is the union of the power sets abc , abd , acd (plus the set abcd * itself).
P(`abcd`) = {`abcd`} + P(`abc`) + P(`abd`) + P(`acd`) + P(`bcd`)
* Note that the empty set that is a member of P (abcd) is also a member of P (abc), P (abd), ... therefore the equivalence indicated above holds.
Recursively, P ( abc ) = { abc } + P ( ab ) + P ( ac ), etc.
The first approach in pseudo code might be:
powerset(string) { add string to set; for each char in string { let substring = string excluding char, add powerset(substring) to set } return set; }
Recursion ends when the row is empty (because it never enters the loop).
If you really need no loops, you will have to convert this loop to another recursion. Now we want to generate ab , ac and cb from abc
powerset(string) { add string to set; add powerset2(string,0) to set; return set } powerset2(string,pos) { if pos<length(string) then let substring = (string excluding the char at pos) add powerset(substring) to set add powerset2(string,pos+1) to set else add "" to set endif return set }
Another approach implements a recursive function P , which either removes the first character from its argument, or does not. (Here + means set union,. Means concatenation, and λ is an empty string)
P(abcd) = P(bcd) + aP(bcd) P(bcd) = P(cd) + bP(cd) P(cd) = P(d) + cP(d) P(d) = λ+d //particular case
Then
P(d) = λ+d R(cd) = P(d) + cP(d) = λ + d + c.(λ+d) = λ + d + c + cd R(bcd) = P(cd) + bP(cd) = λ + d + c + cd + b.(λ + d + c + cd) = λ + d + c + cd + b + bd + bc + bcd P(abcd) = λ + d + c + cd + b + bd + bc + bcd + aλ + ad + ac + acd + ab + abd + abc + abcd
If cycles are enabled, then P disables the power-up function. Otherwise, we need a one-parameter loop function to concatenate the given character into a given set of strings (which, obviously, are two ).
Some tweaking may be possible when playing with String.replace (if you want the result of String or replacing Set with List (so the "extra" parameter is actually the first item in the list).