I know that “a powerset is simply any number between 0 and 2^N-1 where N is number of set members and one in binary presentation denotes presence of corresponding member”.
I would like to generate a powerset using this mapping from the binary representation to the actual set elements.
How can I do this with Erlang?
I have tried to modify this, but with no success.
UPD: My goal is to write an iterative algorithm that generates a powerset of a set without keeping a stack. I tend to think that binary representation could help me with that.
Here is the successful solution in Ruby, but I need to write it in Erlang.
UPD2: Here is the solution in pseudocode, I would like to make something similar in Erlang.
First of all, I would note that with Erlang a recursive solution does not necessarily imply it will consume extra stack. When a method is tail-recursive (i.e., the last thing it does is the recursive call), the compiler will re-write it into modifying the parameters followed by a jump to the beginning of the method. This is fairly standard for functional languages.
To generate a list of all the numbers A to B, use the library method
lists:seq(A, B).To translate a list of values (such as the list from 0 to 2^N-1) into another list of values (such as the set generated from its binary representation), use
lists:mapor a list comprehension.Instead of splitting a number into its binary representation, you might want to consider turning that around and checking whether the corresponding bit is set in each M value (in 0 to 2^N-1) by generating a list of power-of-2-bitmasks. Then, you can do a binary AND to see if the bit is set.
Putting all of that together, you get a solution such as:
However, you can also achieve the same thing using tail recursion without consuming stack space. It also doesn’t need to generate or keep around the list from 0 to 2^N-1.
Note that when generating the list of subsets, you’ll want to put new elements at the head of the list. Lists are singly-linked and immutable, so if you want to put an element anywhere but the beginning, it has to update the “next” pointers, which causes the list to be copied. That’s why the helper function puts the
Acclist at the tail instead of doingAcc ++ [generate_subset(...)]. In this case, since we’re counting down instead of up, we’re already going backwards, so it ends up coming out in the same order.So, in conclusion,
lists:map.[NewElement | ExistingList]) for efficiency reasons.lists:nth) since lists are singly-linked: it would have to iterate the list over and over again. Instead, find a way to iterate the list once, such as how I pre-processed the bit masks above.