Generate all permutations of a list including diferent sizes and repeated elements









up vote
0
down vote

favorite












I wanted to create the function genAllSize ::[a] -> [[a]], that receives a list l and generates all the lists sorted by size that can be built with the elements of the list l; i.e.



> genAllSize [2,4,8] 
[,[2],[4],[8],[2,2],[4,2],[8,2],[2,4],[4,4],[8,4],[2,8],[4,8],[8,8],[2,2,2],[4,2,2],[8,2,2], ...


How would you do it? I came up with a solution using permutations from Data.List but I do not want to use it.










share|improve this question



















  • 1




    filterM (_ -> [False, True]) [2,4,8]
    – Willem Van Onsem
    Nov 5 at 15:44










  • @WillemVanOnsem That doesn't select with replacement.
    – chepner
    Nov 5 at 15:46










  • @chepner: aarghh, I missed that part :(
    – Willem Van Onsem
    Nov 5 at 15:48










  • That solution does not take in account the possibility of repeated numbers. I did not want to post my solution because it was pretty ugly tho... I even came up with a solution without permutations and it's pretty ugly too
    – Adrià Cabeza Sant'Anna
    Nov 5 at 16:07










  • genAllSize x = concat(iterate (z -> genAllSize' z x) []) where genAllSize' l li = foldl (t i -> t++(map(s -> s++[i]) l ) ) [ ] li
    – Adrià Cabeza Sant'Anna
    Nov 5 at 16:09














up vote
0
down vote

favorite












I wanted to create the function genAllSize ::[a] -> [[a]], that receives a list l and generates all the lists sorted by size that can be built with the elements of the list l; i.e.



> genAllSize [2,4,8] 
[,[2],[4],[8],[2,2],[4,2],[8,2],[2,4],[4,4],[8,4],[2,8],[4,8],[8,8],[2,2,2],[4,2,2],[8,2,2], ...


How would you do it? I came up with a solution using permutations from Data.List but I do not want to use it.










share|improve this question



















  • 1




    filterM (_ -> [False, True]) [2,4,8]
    – Willem Van Onsem
    Nov 5 at 15:44










  • @WillemVanOnsem That doesn't select with replacement.
    – chepner
    Nov 5 at 15:46










  • @chepner: aarghh, I missed that part :(
    – Willem Van Onsem
    Nov 5 at 15:48










  • That solution does not take in account the possibility of repeated numbers. I did not want to post my solution because it was pretty ugly tho... I even came up with a solution without permutations and it's pretty ugly too
    – Adrià Cabeza Sant'Anna
    Nov 5 at 16:07










  • genAllSize x = concat(iterate (z -> genAllSize' z x) []) where genAllSize' l li = foldl (t i -> t++(map(s -> s++[i]) l ) ) [ ] li
    – Adrià Cabeza Sant'Anna
    Nov 5 at 16:09












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I wanted to create the function genAllSize ::[a] -> [[a]], that receives a list l and generates all the lists sorted by size that can be built with the elements of the list l; i.e.



> genAllSize [2,4,8] 
[,[2],[4],[8],[2,2],[4,2],[8,2],[2,4],[4,4],[8,4],[2,8],[4,8],[8,8],[2,2,2],[4,2,2],[8,2,2], ...


How would you do it? I came up with a solution using permutations from Data.List but I do not want to use it.










share|improve this question















I wanted to create the function genAllSize ::[a] -> [[a]], that receives a list l and generates all the lists sorted by size that can be built with the elements of the list l; i.e.



> genAllSize [2,4,8] 
[,[2],[4],[8],[2,2],[4,2],[8,2],[2,4],[4,4],[8,4],[2,8],[4,8],[8,8],[2,2,2],[4,2,2],[8,2,2], ...


How would you do it? I came up with a solution using permutations from Data.List but I do not want to use it.







list haskell functional-programming permutation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 7:10









Will Ness

42.3k467118




42.3k467118










asked Nov 5 at 15:40









Adrià Cabeza Sant'Anna

174




174







  • 1




    filterM (_ -> [False, True]) [2,4,8]
    – Willem Van Onsem
    Nov 5 at 15:44










  • @WillemVanOnsem That doesn't select with replacement.
    – chepner
    Nov 5 at 15:46










  • @chepner: aarghh, I missed that part :(
    – Willem Van Onsem
    Nov 5 at 15:48










  • That solution does not take in account the possibility of repeated numbers. I did not want to post my solution because it was pretty ugly tho... I even came up with a solution without permutations and it's pretty ugly too
    – Adrià Cabeza Sant'Anna
    Nov 5 at 16:07










  • genAllSize x = concat(iterate (z -> genAllSize' z x) []) where genAllSize' l li = foldl (t i -> t++(map(s -> s++[i]) l ) ) [ ] li
    – Adrià Cabeza Sant'Anna
    Nov 5 at 16:09












  • 1




    filterM (_ -> [False, True]) [2,4,8]
    – Willem Van Onsem
    Nov 5 at 15:44










  • @WillemVanOnsem That doesn't select with replacement.
    – chepner
    Nov 5 at 15:46










  • @chepner: aarghh, I missed that part :(
    – Willem Van Onsem
    Nov 5 at 15:48










  • That solution does not take in account the possibility of repeated numbers. I did not want to post my solution because it was pretty ugly tho... I even came up with a solution without permutations and it's pretty ugly too
    – Adrià Cabeza Sant'Anna
    Nov 5 at 16:07










  • genAllSize x = concat(iterate (z -> genAllSize' z x) []) where genAllSize' l li = foldl (t i -> t++(map(s -> s++[i]) l ) ) [ ] li
    – Adrià Cabeza Sant'Anna
    Nov 5 at 16:09







1




1




filterM (_ -> [False, True]) [2,4,8]
– Willem Van Onsem
Nov 5 at 15:44




filterM (_ -> [False, True]) [2,4,8]
– Willem Van Onsem
Nov 5 at 15:44












@WillemVanOnsem That doesn't select with replacement.
– chepner
Nov 5 at 15:46




@WillemVanOnsem That doesn't select with replacement.
– chepner
Nov 5 at 15:46












@chepner: aarghh, I missed that part :(
– Willem Van Onsem
Nov 5 at 15:48




@chepner: aarghh, I missed that part :(
– Willem Van Onsem
Nov 5 at 15:48












That solution does not take in account the possibility of repeated numbers. I did not want to post my solution because it was pretty ugly tho... I even came up with a solution without permutations and it's pretty ugly too
– Adrià Cabeza Sant'Anna
Nov 5 at 16:07




That solution does not take in account the possibility of repeated numbers. I did not want to post my solution because it was pretty ugly tho... I even came up with a solution without permutations and it's pretty ugly too
– Adrià Cabeza Sant'Anna
Nov 5 at 16:07












genAllSize x = concat(iterate (z -> genAllSize' z x) []) where genAllSize' l li = foldl (t i -> t++(map(s -> s++[i]) l ) ) [ ] li
– Adrià Cabeza Sant'Anna
Nov 5 at 16:09




genAllSize x = concat(iterate (z -> genAllSize' z x) []) where genAllSize' l li = foldl (t i -> t++(map(s -> s++[i]) l ) ) [ ] li
– Adrià Cabeza Sant'Anna
Nov 5 at 16:09












3 Answers
3






active

oldest

votes

















up vote
4
down vote













  • Given an input list xs, select a prefix of that in a non deterministic way

  • For each element in the prefix, replace it with any element of xs, in a non deterministic way

Result:



> xs = [2,4,8]
> inits xs >>= mapM (const xs)
[,[2],[4],[8],[2,2],[2,4],[2,8],[4,2],[4,4],[4,8],[8,2],[8,4],
[8,8],[2,2,2],[2,2,4],[2,2,8],[2,4,2],[2,4,4],[2,4,8],[2,8,2],
[2,8,4],[2,8,8],[4,2,2],[4,2,4],[4,2,8],[4,4,2],[4,4,4],[4,4,8],
[4,8,2],[4,8,4],[4,8,8],[8,2,2],[8,2,4],[8,2,8],[8,4,2],[8,4,4],
[8,4,8],[8,8,2],[8,8,4],[8,8,8]]





share|improve this answer
















  • 1




    why is it finite?
    – karakfa
    Nov 5 at 16:37










  • @karakfa We only involve finite lists above. xs is finite, so inits xs is finite, and >>= loops over a finite number of finite prefixes. mapM loops over each element (finitely many), and replaces them with an element taken from xs (finite).
    – chi
    Nov 5 at 17:07






  • 1




    This doesn’t actually use the elements of the results of inits, just their lengths, so you could write this a bit more explicitly (albeit less elegantly imo) as join [sequence $ take n $ repeat xs | n <- [0..length xs]]. Also it’s worth knowing that mapM (const xs) = sequence . fmap (const xs) = sequence . (xs <$); the expression x <$ y is handy for replacing all elements in the container y with x while retaining its shape, or running an action y, discarding its result, and returning x (where you might otherwise write y *> pure x).
    – Jon Purdy
    Nov 6 at 0:56

















up vote
2
down vote













Hmm I guess you a need a lazy infinite list of cycling subsequences. One naive way could be like



Prelude> take 100 $ nub . subsequences . cycle $ [2,4,8]
[,[2],[4],[2,4],[8],[2,8],[4,8],[2,4,8],[2,2],[4,2],[2,4,2],[8,2],[2,8,2],[4,8,2],[2,4,8,2],[4,4],[2,4,4],[8,4],[2,8,4],[4,8,4],[2,4,8,4],[2,2,4],[4,2,4],[2,4,2,4],[8,2,4],[2,8,2,4],[4,8,2,4],[2,4,8,2,4],[8,8],[2,8,8],[4,8,8],[2,4,8,8],[2,2,8],[4,2,8],[2,4,2,8],[8,2,8],[2,8,2,8],[4,8,2,8],[2,4,8,2,8],[4,4,8],[2,4,4,8],[8,4,8],[2,8,4,8],[4,8,4,8],[2,4,8,4,8],[2,2,4,8],[4,2,4,8],[2,4,2,4,8],[8,2,4,8],[2,8,2,4,8],[4,8,2,4,8],[2,4,8,2,4,8],[2,2,2],[4,2,2],[2,4,2,2],[8,2,2],[2,8,2,2],[4,8,2,2],[2,4,8,2,2],[4,4,2],[2,4,4,2],[8,4,2],[2,8,4,2],[4,8,4,2],[2,4,8,4,2],[2,2,4,2],[4,2,4,2],[2,4,2,4,2],[8,2,4,2],[2,8,2,4,2],[4,8,2,4,2],[2,4,8,2,4,2]]





share|improve this answer



























    up vote
    0
    down vote













    This produces the elements in a different order within each length than your example, but it meets the definition of the text of your question. Changing the order is easy - you have to replace <*> with a slightly different operator of your own making.



    import Control.Applicative
    import Control.Monad

    rinvjoin :: Applicative both => both a -> both (both a)
    rinvjoin = fmap pure

    extendBranches options branches = (<|>) <$> options <*> branches
    singletonBranchExtensions = rinvjoin

    genAllSize =
    genAllSize xs = join <$> iterate (extendBranches extensions) $ initialBranches
    where extensions = singletonBranchExtensions xs
    initialBranches = pure empty





    share|improve this answer






















    • indeed. But I'm going to leave it because I get confused by the myriad of maximally refined concepts and I bet others do too.
      – codeshot
      Nov 11 at 11:06










    • My current solution has a problem that it keeps looking for branch extensions forever for genAllSize but where there's a finite answer, the list should be finite, really
      – codeshot
      Nov 11 at 11:07










    • genAllSize xs = join $ iterate (liftA2 (<|>) (pure <$> xs)) (pure empty) is write-only, though. Mine needs to be expanded out into more explicit definitions as it is, removing some can't be right.
      – codeshot
      Nov 11 at 11:09










    • It's not supposed to do what chi's answer is doing, chi's answer isn't correct
      – codeshot
      Nov 11 at 11:13










    • [2,2,2,2] is a list made from some combination of elements from [2,4,8] and yet chi's answer doesn't include it in the output list. My answer correctly includes it.
      – codeshot
      Nov 11 at 11:22










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53157534%2fgenerate-all-permutations-of-a-list-including-diferent-sizes-and-repeated-elemen%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote













    • Given an input list xs, select a prefix of that in a non deterministic way

    • For each element in the prefix, replace it with any element of xs, in a non deterministic way

    Result:



    > xs = [2,4,8]
    > inits xs >>= mapM (const xs)
    [,[2],[4],[8],[2,2],[2,4],[2,8],[4,2],[4,4],[4,8],[8,2],[8,4],
    [8,8],[2,2,2],[2,2,4],[2,2,8],[2,4,2],[2,4,4],[2,4,8],[2,8,2],
    [2,8,4],[2,8,8],[4,2,2],[4,2,4],[4,2,8],[4,4,2],[4,4,4],[4,4,8],
    [4,8,2],[4,8,4],[4,8,8],[8,2,2],[8,2,4],[8,2,8],[8,4,2],[8,4,4],
    [8,4,8],[8,8,2],[8,8,4],[8,8,8]]





    share|improve this answer
















    • 1




      why is it finite?
      – karakfa
      Nov 5 at 16:37










    • @karakfa We only involve finite lists above. xs is finite, so inits xs is finite, and >>= loops over a finite number of finite prefixes. mapM loops over each element (finitely many), and replaces them with an element taken from xs (finite).
      – chi
      Nov 5 at 17:07






    • 1




      This doesn’t actually use the elements of the results of inits, just their lengths, so you could write this a bit more explicitly (albeit less elegantly imo) as join [sequence $ take n $ repeat xs | n <- [0..length xs]]. Also it’s worth knowing that mapM (const xs) = sequence . fmap (const xs) = sequence . (xs <$); the expression x <$ y is handy for replacing all elements in the container y with x while retaining its shape, or running an action y, discarding its result, and returning x (where you might otherwise write y *> pure x).
      – Jon Purdy
      Nov 6 at 0:56














    up vote
    4
    down vote













    • Given an input list xs, select a prefix of that in a non deterministic way

    • For each element in the prefix, replace it with any element of xs, in a non deterministic way

    Result:



    > xs = [2,4,8]
    > inits xs >>= mapM (const xs)
    [,[2],[4],[8],[2,2],[2,4],[2,8],[4,2],[4,4],[4,8],[8,2],[8,4],
    [8,8],[2,2,2],[2,2,4],[2,2,8],[2,4,2],[2,4,4],[2,4,8],[2,8,2],
    [2,8,4],[2,8,8],[4,2,2],[4,2,4],[4,2,8],[4,4,2],[4,4,4],[4,4,8],
    [4,8,2],[4,8,4],[4,8,8],[8,2,2],[8,2,4],[8,2,8],[8,4,2],[8,4,4],
    [8,4,8],[8,8,2],[8,8,4],[8,8,8]]





    share|improve this answer
















    • 1




      why is it finite?
      – karakfa
      Nov 5 at 16:37










    • @karakfa We only involve finite lists above. xs is finite, so inits xs is finite, and >>= loops over a finite number of finite prefixes. mapM loops over each element (finitely many), and replaces them with an element taken from xs (finite).
      – chi
      Nov 5 at 17:07






    • 1




      This doesn’t actually use the elements of the results of inits, just their lengths, so you could write this a bit more explicitly (albeit less elegantly imo) as join [sequence $ take n $ repeat xs | n <- [0..length xs]]. Also it’s worth knowing that mapM (const xs) = sequence . fmap (const xs) = sequence . (xs <$); the expression x <$ y is handy for replacing all elements in the container y with x while retaining its shape, or running an action y, discarding its result, and returning x (where you might otherwise write y *> pure x).
      – Jon Purdy
      Nov 6 at 0:56












    up vote
    4
    down vote










    up vote
    4
    down vote









    • Given an input list xs, select a prefix of that in a non deterministic way

    • For each element in the prefix, replace it with any element of xs, in a non deterministic way

    Result:



    > xs = [2,4,8]
    > inits xs >>= mapM (const xs)
    [,[2],[4],[8],[2,2],[2,4],[2,8],[4,2],[4,4],[4,8],[8,2],[8,4],
    [8,8],[2,2,2],[2,2,4],[2,2,8],[2,4,2],[2,4,4],[2,4,8],[2,8,2],
    [2,8,4],[2,8,8],[4,2,2],[4,2,4],[4,2,8],[4,4,2],[4,4,4],[4,4,8],
    [4,8,2],[4,8,4],[4,8,8],[8,2,2],[8,2,4],[8,2,8],[8,4,2],[8,4,4],
    [8,4,8],[8,8,2],[8,8,4],[8,8,8]]





    share|improve this answer












    • Given an input list xs, select a prefix of that in a non deterministic way

    • For each element in the prefix, replace it with any element of xs, in a non deterministic way

    Result:



    > xs = [2,4,8]
    > inits xs >>= mapM (const xs)
    [,[2],[4],[8],[2,2],[2,4],[2,8],[4,2],[4,4],[4,8],[8,2],[8,4],
    [8,8],[2,2,2],[2,2,4],[2,2,8],[2,4,2],[2,4,4],[2,4,8],[2,8,2],
    [2,8,4],[2,8,8],[4,2,2],[4,2,4],[4,2,8],[4,4,2],[4,4,4],[4,4,8],
    [4,8,2],[4,8,4],[4,8,8],[8,2,2],[8,2,4],[8,2,8],[8,4,2],[8,4,4],
    [8,4,8],[8,8,2],[8,8,4],[8,8,8]]






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 5 at 16:09









    chi

    72.3k280133




    72.3k280133







    • 1




      why is it finite?
      – karakfa
      Nov 5 at 16:37










    • @karakfa We only involve finite lists above. xs is finite, so inits xs is finite, and >>= loops over a finite number of finite prefixes. mapM loops over each element (finitely many), and replaces them with an element taken from xs (finite).
      – chi
      Nov 5 at 17:07






    • 1




      This doesn’t actually use the elements of the results of inits, just their lengths, so you could write this a bit more explicitly (albeit less elegantly imo) as join [sequence $ take n $ repeat xs | n <- [0..length xs]]. Also it’s worth knowing that mapM (const xs) = sequence . fmap (const xs) = sequence . (xs <$); the expression x <$ y is handy for replacing all elements in the container y with x while retaining its shape, or running an action y, discarding its result, and returning x (where you might otherwise write y *> pure x).
      – Jon Purdy
      Nov 6 at 0:56












    • 1




      why is it finite?
      – karakfa
      Nov 5 at 16:37










    • @karakfa We only involve finite lists above. xs is finite, so inits xs is finite, and >>= loops over a finite number of finite prefixes. mapM loops over each element (finitely many), and replaces them with an element taken from xs (finite).
      – chi
      Nov 5 at 17:07






    • 1




      This doesn’t actually use the elements of the results of inits, just their lengths, so you could write this a bit more explicitly (albeit less elegantly imo) as join [sequence $ take n $ repeat xs | n <- [0..length xs]]. Also it’s worth knowing that mapM (const xs) = sequence . fmap (const xs) = sequence . (xs <$); the expression x <$ y is handy for replacing all elements in the container y with x while retaining its shape, or running an action y, discarding its result, and returning x (where you might otherwise write y *> pure x).
      – Jon Purdy
      Nov 6 at 0:56







    1




    1




    why is it finite?
    – karakfa
    Nov 5 at 16:37




    why is it finite?
    – karakfa
    Nov 5 at 16:37












    @karakfa We only involve finite lists above. xs is finite, so inits xs is finite, and >>= loops over a finite number of finite prefixes. mapM loops over each element (finitely many), and replaces them with an element taken from xs (finite).
    – chi
    Nov 5 at 17:07




    @karakfa We only involve finite lists above. xs is finite, so inits xs is finite, and >>= loops over a finite number of finite prefixes. mapM loops over each element (finitely many), and replaces them with an element taken from xs (finite).
    – chi
    Nov 5 at 17:07




    1




    1




    This doesn’t actually use the elements of the results of inits, just their lengths, so you could write this a bit more explicitly (albeit less elegantly imo) as join [sequence $ take n $ repeat xs | n <- [0..length xs]]. Also it’s worth knowing that mapM (const xs) = sequence . fmap (const xs) = sequence . (xs <$); the expression x <$ y is handy for replacing all elements in the container y with x while retaining its shape, or running an action y, discarding its result, and returning x (where you might otherwise write y *> pure x).
    – Jon Purdy
    Nov 6 at 0:56




    This doesn’t actually use the elements of the results of inits, just their lengths, so you could write this a bit more explicitly (albeit less elegantly imo) as join [sequence $ take n $ repeat xs | n <- [0..length xs]]. Also it’s worth knowing that mapM (const xs) = sequence . fmap (const xs) = sequence . (xs <$); the expression x <$ y is handy for replacing all elements in the container y with x while retaining its shape, or running an action y, discarding its result, and returning x (where you might otherwise write y *> pure x).
    – Jon Purdy
    Nov 6 at 0:56












    up vote
    2
    down vote













    Hmm I guess you a need a lazy infinite list of cycling subsequences. One naive way could be like



    Prelude> take 100 $ nub . subsequences . cycle $ [2,4,8]
    [,[2],[4],[2,4],[8],[2,8],[4,8],[2,4,8],[2,2],[4,2],[2,4,2],[8,2],[2,8,2],[4,8,2],[2,4,8,2],[4,4],[2,4,4],[8,4],[2,8,4],[4,8,4],[2,4,8,4],[2,2,4],[4,2,4],[2,4,2,4],[8,2,4],[2,8,2,4],[4,8,2,4],[2,4,8,2,4],[8,8],[2,8,8],[4,8,8],[2,4,8,8],[2,2,8],[4,2,8],[2,4,2,8],[8,2,8],[2,8,2,8],[4,8,2,8],[2,4,8,2,8],[4,4,8],[2,4,4,8],[8,4,8],[2,8,4,8],[4,8,4,8],[2,4,8,4,8],[2,2,4,8],[4,2,4,8],[2,4,2,4,8],[8,2,4,8],[2,8,2,4,8],[4,8,2,4,8],[2,4,8,2,4,8],[2,2,2],[4,2,2],[2,4,2,2],[8,2,2],[2,8,2,2],[4,8,2,2],[2,4,8,2,2],[4,4,2],[2,4,4,2],[8,4,2],[2,8,4,2],[4,8,4,2],[2,4,8,4,2],[2,2,4,2],[4,2,4,2],[2,4,2,4,2],[8,2,4,2],[2,8,2,4,2],[4,8,2,4,2],[2,4,8,2,4,2]]





    share|improve this answer
























      up vote
      2
      down vote













      Hmm I guess you a need a lazy infinite list of cycling subsequences. One naive way could be like



      Prelude> take 100 $ nub . subsequences . cycle $ [2,4,8]
      [,[2],[4],[2,4],[8],[2,8],[4,8],[2,4,8],[2,2],[4,2],[2,4,2],[8,2],[2,8,2],[4,8,2],[2,4,8,2],[4,4],[2,4,4],[8,4],[2,8,4],[4,8,4],[2,4,8,4],[2,2,4],[4,2,4],[2,4,2,4],[8,2,4],[2,8,2,4],[4,8,2,4],[2,4,8,2,4],[8,8],[2,8,8],[4,8,8],[2,4,8,8],[2,2,8],[4,2,8],[2,4,2,8],[8,2,8],[2,8,2,8],[4,8,2,8],[2,4,8,2,8],[4,4,8],[2,4,4,8],[8,4,8],[2,8,4,8],[4,8,4,8],[2,4,8,4,8],[2,2,4,8],[4,2,4,8],[2,4,2,4,8],[8,2,4,8],[2,8,2,4,8],[4,8,2,4,8],[2,4,8,2,4,8],[2,2,2],[4,2,2],[2,4,2,2],[8,2,2],[2,8,2,2],[4,8,2,2],[2,4,8,2,2],[4,4,2],[2,4,4,2],[8,4,2],[2,8,4,2],[4,8,4,2],[2,4,8,4,2],[2,2,4,2],[4,2,4,2],[2,4,2,4,2],[8,2,4,2],[2,8,2,4,2],[4,8,2,4,2],[2,4,8,2,4,2]]





      share|improve this answer






















        up vote
        2
        down vote










        up vote
        2
        down vote









        Hmm I guess you a need a lazy infinite list of cycling subsequences. One naive way could be like



        Prelude> take 100 $ nub . subsequences . cycle $ [2,4,8]
        [,[2],[4],[2,4],[8],[2,8],[4,8],[2,4,8],[2,2],[4,2],[2,4,2],[8,2],[2,8,2],[4,8,2],[2,4,8,2],[4,4],[2,4,4],[8,4],[2,8,4],[4,8,4],[2,4,8,4],[2,2,4],[4,2,4],[2,4,2,4],[8,2,4],[2,8,2,4],[4,8,2,4],[2,4,8,2,4],[8,8],[2,8,8],[4,8,8],[2,4,8,8],[2,2,8],[4,2,8],[2,4,2,8],[8,2,8],[2,8,2,8],[4,8,2,8],[2,4,8,2,8],[4,4,8],[2,4,4,8],[8,4,8],[2,8,4,8],[4,8,4,8],[2,4,8,4,8],[2,2,4,8],[4,2,4,8],[2,4,2,4,8],[8,2,4,8],[2,8,2,4,8],[4,8,2,4,8],[2,4,8,2,4,8],[2,2,2],[4,2,2],[2,4,2,2],[8,2,2],[2,8,2,2],[4,8,2,2],[2,4,8,2,2],[4,4,2],[2,4,4,2],[8,4,2],[2,8,4,2],[4,8,4,2],[2,4,8,4,2],[2,2,4,2],[4,2,4,2],[2,4,2,4,2],[8,2,4,2],[2,8,2,4,2],[4,8,2,4,2],[2,4,8,2,4,2]]





        share|improve this answer












        Hmm I guess you a need a lazy infinite list of cycling subsequences. One naive way could be like



        Prelude> take 100 $ nub . subsequences . cycle $ [2,4,8]
        [,[2],[4],[2,4],[8],[2,8],[4,8],[2,4,8],[2,2],[4,2],[2,4,2],[8,2],[2,8,2],[4,8,2],[2,4,8,2],[4,4],[2,4,4],[8,4],[2,8,4],[4,8,4],[2,4,8,4],[2,2,4],[4,2,4],[2,4,2,4],[8,2,4],[2,8,2,4],[4,8,2,4],[2,4,8,2,4],[8,8],[2,8,8],[4,8,8],[2,4,8,8],[2,2,8],[4,2,8],[2,4,2,8],[8,2,8],[2,8,2,8],[4,8,2,8],[2,4,8,2,8],[4,4,8],[2,4,4,8],[8,4,8],[2,8,4,8],[4,8,4,8],[2,4,8,4,8],[2,2,4,8],[4,2,4,8],[2,4,2,4,8],[8,2,4,8],[2,8,2,4,8],[4,8,2,4,8],[2,4,8,2,4,8],[2,2,2],[4,2,2],[2,4,2,2],[8,2,2],[2,8,2,2],[4,8,2,2],[2,4,8,2,2],[4,4,2],[2,4,4,2],[8,4,2],[2,8,4,2],[4,8,4,2],[2,4,8,4,2],[2,2,4,2],[4,2,4,2],[2,4,2,4,2],[8,2,4,2],[2,8,2,4,2],[4,8,2,4,2],[2,4,8,2,4,2]]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 5 at 17:16









        Redu

        12.5k22435




        12.5k22435




















            up vote
            0
            down vote













            This produces the elements in a different order within each length than your example, but it meets the definition of the text of your question. Changing the order is easy - you have to replace <*> with a slightly different operator of your own making.



            import Control.Applicative
            import Control.Monad

            rinvjoin :: Applicative both => both a -> both (both a)
            rinvjoin = fmap pure

            extendBranches options branches = (<|>) <$> options <*> branches
            singletonBranchExtensions = rinvjoin

            genAllSize =
            genAllSize xs = join <$> iterate (extendBranches extensions) $ initialBranches
            where extensions = singletonBranchExtensions xs
            initialBranches = pure empty





            share|improve this answer






















            • indeed. But I'm going to leave it because I get confused by the myriad of maximally refined concepts and I bet others do too.
              – codeshot
              Nov 11 at 11:06










            • My current solution has a problem that it keeps looking for branch extensions forever for genAllSize but where there's a finite answer, the list should be finite, really
              – codeshot
              Nov 11 at 11:07










            • genAllSize xs = join $ iterate (liftA2 (<|>) (pure <$> xs)) (pure empty) is write-only, though. Mine needs to be expanded out into more explicit definitions as it is, removing some can't be right.
              – codeshot
              Nov 11 at 11:09










            • It's not supposed to do what chi's answer is doing, chi's answer isn't correct
              – codeshot
              Nov 11 at 11:13










            • [2,2,2,2] is a list made from some combination of elements from [2,4,8] and yet chi's answer doesn't include it in the output list. My answer correctly includes it.
              – codeshot
              Nov 11 at 11:22














            up vote
            0
            down vote













            This produces the elements in a different order within each length than your example, but it meets the definition of the text of your question. Changing the order is easy - you have to replace <*> with a slightly different operator of your own making.



            import Control.Applicative
            import Control.Monad

            rinvjoin :: Applicative both => both a -> both (both a)
            rinvjoin = fmap pure

            extendBranches options branches = (<|>) <$> options <*> branches
            singletonBranchExtensions = rinvjoin

            genAllSize =
            genAllSize xs = join <$> iterate (extendBranches extensions) $ initialBranches
            where extensions = singletonBranchExtensions xs
            initialBranches = pure empty





            share|improve this answer






















            • indeed. But I'm going to leave it because I get confused by the myriad of maximally refined concepts and I bet others do too.
              – codeshot
              Nov 11 at 11:06










            • My current solution has a problem that it keeps looking for branch extensions forever for genAllSize but where there's a finite answer, the list should be finite, really
              – codeshot
              Nov 11 at 11:07










            • genAllSize xs = join $ iterate (liftA2 (<|>) (pure <$> xs)) (pure empty) is write-only, though. Mine needs to be expanded out into more explicit definitions as it is, removing some can't be right.
              – codeshot
              Nov 11 at 11:09










            • It's not supposed to do what chi's answer is doing, chi's answer isn't correct
              – codeshot
              Nov 11 at 11:13










            • [2,2,2,2] is a list made from some combination of elements from [2,4,8] and yet chi's answer doesn't include it in the output list. My answer correctly includes it.
              – codeshot
              Nov 11 at 11:22












            up vote
            0
            down vote










            up vote
            0
            down vote









            This produces the elements in a different order within each length than your example, but it meets the definition of the text of your question. Changing the order is easy - you have to replace <*> with a slightly different operator of your own making.



            import Control.Applicative
            import Control.Monad

            rinvjoin :: Applicative both => both a -> both (both a)
            rinvjoin = fmap pure

            extendBranches options branches = (<|>) <$> options <*> branches
            singletonBranchExtensions = rinvjoin

            genAllSize =
            genAllSize xs = join <$> iterate (extendBranches extensions) $ initialBranches
            where extensions = singletonBranchExtensions xs
            initialBranches = pure empty





            share|improve this answer














            This produces the elements in a different order within each length than your example, but it meets the definition of the text of your question. Changing the order is easy - you have to replace <*> with a slightly different operator of your own making.



            import Control.Applicative
            import Control.Monad

            rinvjoin :: Applicative both => both a -> both (both a)
            rinvjoin = fmap pure

            extendBranches options branches = (<|>) <$> options <*> branches
            singletonBranchExtensions = rinvjoin

            genAllSize =
            genAllSize xs = join <$> iterate (extendBranches extensions) $ initialBranches
            where extensions = singletonBranchExtensions xs
            initialBranches = pure empty






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 11 at 11:30

























            answered Nov 11 at 10:37









            codeshot

            7231718




            7231718











            • indeed. But I'm going to leave it because I get confused by the myriad of maximally refined concepts and I bet others do too.
              – codeshot
              Nov 11 at 11:06










            • My current solution has a problem that it keeps looking for branch extensions forever for genAllSize but where there's a finite answer, the list should be finite, really
              – codeshot
              Nov 11 at 11:07










            • genAllSize xs = join $ iterate (liftA2 (<|>) (pure <$> xs)) (pure empty) is write-only, though. Mine needs to be expanded out into more explicit definitions as it is, removing some can't be right.
              – codeshot
              Nov 11 at 11:09










            • It's not supposed to do what chi's answer is doing, chi's answer isn't correct
              – codeshot
              Nov 11 at 11:13










            • [2,2,2,2] is a list made from some combination of elements from [2,4,8] and yet chi's answer doesn't include it in the output list. My answer correctly includes it.
              – codeshot
              Nov 11 at 11:22
















            • indeed. But I'm going to leave it because I get confused by the myriad of maximally refined concepts and I bet others do too.
              – codeshot
              Nov 11 at 11:06










            • My current solution has a problem that it keeps looking for branch extensions forever for genAllSize but where there's a finite answer, the list should be finite, really
              – codeshot
              Nov 11 at 11:07










            • genAllSize xs = join $ iterate (liftA2 (<|>) (pure <$> xs)) (pure empty) is write-only, though. Mine needs to be expanded out into more explicit definitions as it is, removing some can't be right.
              – codeshot
              Nov 11 at 11:09










            • It's not supposed to do what chi's answer is doing, chi's answer isn't correct
              – codeshot
              Nov 11 at 11:13










            • [2,2,2,2] is a list made from some combination of elements from [2,4,8] and yet chi's answer doesn't include it in the output list. My answer correctly includes it.
              – codeshot
              Nov 11 at 11:22















            indeed. But I'm going to leave it because I get confused by the myriad of maximally refined concepts and I bet others do too.
            – codeshot
            Nov 11 at 11:06




            indeed. But I'm going to leave it because I get confused by the myriad of maximally refined concepts and I bet others do too.
            – codeshot
            Nov 11 at 11:06












            My current solution has a problem that it keeps looking for branch extensions forever for genAllSize but where there's a finite answer, the list should be finite, really
            – codeshot
            Nov 11 at 11:07




            My current solution has a problem that it keeps looking for branch extensions forever for genAllSize but where there's a finite answer, the list should be finite, really
            – codeshot
            Nov 11 at 11:07












            genAllSize xs = join $ iterate (liftA2 (<|>) (pure <$> xs)) (pure empty) is write-only, though. Mine needs to be expanded out into more explicit definitions as it is, removing some can't be right.
            – codeshot
            Nov 11 at 11:09




            genAllSize xs = join $ iterate (liftA2 (<|>) (pure <$> xs)) (pure empty) is write-only, though. Mine needs to be expanded out into more explicit definitions as it is, removing some can't be right.
            – codeshot
            Nov 11 at 11:09












            It's not supposed to do what chi's answer is doing, chi's answer isn't correct
            – codeshot
            Nov 11 at 11:13




            It's not supposed to do what chi's answer is doing, chi's answer isn't correct
            – codeshot
            Nov 11 at 11:13












            [2,2,2,2] is a list made from some combination of elements from [2,4,8] and yet chi's answer doesn't include it in the output list. My answer correctly includes it.
            – codeshot
            Nov 11 at 11:22




            [2,2,2,2] is a list made from some combination of elements from [2,4,8] and yet chi's answer doesn't include it in the output list. My answer correctly includes it.
            – codeshot
            Nov 11 at 11:22

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53157534%2fgenerate-all-permutations-of-a-list-including-diferent-sizes-and-repeated-elemen%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党