Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6992499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:38:31+00:00 2026-05-27T19:38:31+00:00

I know that a powerset is simply any number between 0 and 2^N-1 where

  • 0

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”.

(Hynek -Pichi- Vychodil)

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:38:32+00:00Added an answer on May 27, 2026 at 7:38 pm

    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:map or 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:

    generate_powerset(List) ->
        % Do some pre-processing of the list to help with checks later.
        % This involves modifying the list to combine the element with
        % the bitmask it will need later on, such as:
        % [a, b, c, d, e] ==> [{1,a}, {2,b}, {4,c}, {8,d}, {16,e}]
        PowersOf2 = [1 bsl (X-1) || X <- lists:seq(1, length(List))],
        ListWithMasks = lists:zip(PowersOf2, List),
    
        % Generate the list from 0 to 1^N - 1
        AllMs = lists:seq(0, (1 bsl length(List)) - 1),
    
        % For each value, generate the corresponding subset
        lists:map(fun (M) -> generate_subset(M, ListWithMasks) end, AllMs).
        % or, using a list comprehension:
        % [generate_subset(M, ListWithMasks) || M <- AllMs].
    
    generate_subset(M, ListWithMasks) ->
        % List comprehension: choose each element where the Mask value has
        % the corresponding bit set in M.
        [Element || {Mask, Element} <- ListWithMasks, M band Mask =/= 0].
    

    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.

    generate_powerset(List) ->
        % same preliminary steps as above...
        PowersOf2 = [1 bsl (X-1) || X <- lists:seq(1, length(List))],
        ListWithMasks = lists:zip(PowersOf2, List),
        % call tail-recursive helper method -- it can have the same name
        % as long as it has different arity.
        generate_powerset(ListWithMasks, (1 bsl length(List)) - 1, []).
    
    generate_powerset(_ListWithMasks, -1, Acc) -> Acc;
    generate_powerset(ListWithMasks, M, Acc) ->
        generate_powerset(ListWithMasks, M-1,
                          [generate_subset(M, ListWithMasks) | Acc]).
    
    % same as above...
    generate_subset(M, ListWithMasks) ->
        [Element || {Mask, Element} <- ListWithMasks, M band Mask =/= 0].
    

    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 Acc list at the tail instead of doing Acc ++ [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,

    1. Looping in Erlang is idiomatically done via a tail recursive function or using a variation of lists:map.
    2. In many (most?) functional languages, including Erlang, tail recursion does not consume extra stack space since it is implemented using jumps.
    3. List construction is typically done backwards (i.e., [NewElement | ExistingList]) for efficiency reasons.
    4. You generally don’t want to find the Nth item in a list (using 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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that you can set the number of threads to use for all
I know that ads colors can be set up in account settings, but I
I know that, as of Java 1.5 , one can add a component to
I know that some of the Microsoft employees are members of StackOverflow like the
I know that Apple does not permit developers to read the phone number of
I know that the finfo function in SAS returns filesize as one of the
I know that I can add more than one project to a solution, but
I know that there has been one question about this but it is not
I know that in general, we want to check for the presence of specific
I know that standards define two versions of ODT file: - one is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.