List comprehension stop generating certain lists if sublist fails on check









up vote
0
down vote

favorite












I have a code, which generates all the possible variation (with the legnth of N) with repetition.



variation(1, L) ->
[ [H] || H <- L ];
variation(N, L) ->
[[H | T] || H <- L, T <- variation(N - 1, L)].


For variation(3, [1,2,3,4]) it will generate:



[[1,1,1,1],[1,1,1,2],[1,1,1,3],[1,1,1,4],[1,1,2,1],...]



I would like to check a condition during the generation of the lists. If a sublist fails, it should stop generating lists, that begins with the certain sublist.
For example if [1,1] sublist fails that condition (check), than it should not generate [1,1,1,1], [1,1,1,2] etc (all of those that begin with [1,1]).



I don't know if its possible with 1 list comprehension.
So far, I have this code:



variation(1, L) ->
[ [H] || H <- L ];
variation(N, L) ->
[[H | T] || H <- L, T <- variation(N - 1, L), check([H|T]) ].


This solution will only return those lists, that doesn't fail the condition (it works, but really slow for big input).



If [1,1] fails, it will try to generate [1,1,1,2], but those will fail the check as well. I would need a solution, which doesn't try to generate lists that begin with [1,1,...] (or with a previously failing sublist).










share|improve this question























  • Can you run the check only on H? Like this… variation(1, L) -> [ [H] || H <- L ]; variation(N, L) -> [[H | T] || H <- L, check(H), T <- variation(N - 1, L) ].
    – Brujo Benavides
    Nov 12 at 11:21











  • I’ll add it as an answer, just for the code formatting :S
    – Brujo Benavides
    Nov 12 at 11:22














up vote
0
down vote

favorite












I have a code, which generates all the possible variation (with the legnth of N) with repetition.



variation(1, L) ->
[ [H] || H <- L ];
variation(N, L) ->
[[H | T] || H <- L, T <- variation(N - 1, L)].


For variation(3, [1,2,3,4]) it will generate:



[[1,1,1,1],[1,1,1,2],[1,1,1,3],[1,1,1,4],[1,1,2,1],...]



I would like to check a condition during the generation of the lists. If a sublist fails, it should stop generating lists, that begins with the certain sublist.
For example if [1,1] sublist fails that condition (check), than it should not generate [1,1,1,1], [1,1,1,2] etc (all of those that begin with [1,1]).



I don't know if its possible with 1 list comprehension.
So far, I have this code:



variation(1, L) ->
[ [H] || H <- L ];
variation(N, L) ->
[[H | T] || H <- L, T <- variation(N - 1, L), check([H|T]) ].


This solution will only return those lists, that doesn't fail the condition (it works, but really slow for big input).



If [1,1] fails, it will try to generate [1,1,1,2], but those will fail the check as well. I would need a solution, which doesn't try to generate lists that begin with [1,1,...] (or with a previously failing sublist).










share|improve this question























  • Can you run the check only on H? Like this… variation(1, L) -> [ [H] || H <- L ]; variation(N, L) -> [[H | T] || H <- L, check(H), T <- variation(N - 1, L) ].
    – Brujo Benavides
    Nov 12 at 11:21











  • I’ll add it as an answer, just for the code formatting :S
    – Brujo Benavides
    Nov 12 at 11:22












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a code, which generates all the possible variation (with the legnth of N) with repetition.



variation(1, L) ->
[ [H] || H <- L ];
variation(N, L) ->
[[H | T] || H <- L, T <- variation(N - 1, L)].


For variation(3, [1,2,3,4]) it will generate:



[[1,1,1,1],[1,1,1,2],[1,1,1,3],[1,1,1,4],[1,1,2,1],...]



I would like to check a condition during the generation of the lists. If a sublist fails, it should stop generating lists, that begins with the certain sublist.
For example if [1,1] sublist fails that condition (check), than it should not generate [1,1,1,1], [1,1,1,2] etc (all of those that begin with [1,1]).



I don't know if its possible with 1 list comprehension.
So far, I have this code:



variation(1, L) ->
[ [H] || H <- L ];
variation(N, L) ->
[[H | T] || H <- L, T <- variation(N - 1, L), check([H|T]) ].


This solution will only return those lists, that doesn't fail the condition (it works, but really slow for big input).



If [1,1] fails, it will try to generate [1,1,1,2], but those will fail the check as well. I would need a solution, which doesn't try to generate lists that begin with [1,1,...] (or with a previously failing sublist).










share|improve this question















I have a code, which generates all the possible variation (with the legnth of N) with repetition.



variation(1, L) ->
[ [H] || H <- L ];
variation(N, L) ->
[[H | T] || H <- L, T <- variation(N - 1, L)].


For variation(3, [1,2,3,4]) it will generate:



[[1,1,1,1],[1,1,1,2],[1,1,1,3],[1,1,1,4],[1,1,2,1],...]



I would like to check a condition during the generation of the lists. If a sublist fails, it should stop generating lists, that begins with the certain sublist.
For example if [1,1] sublist fails that condition (check), than it should not generate [1,1,1,1], [1,1,1,2] etc (all of those that begin with [1,1]).



I don't know if its possible with 1 list comprehension.
So far, I have this code:



variation(1, L) ->
[ [H] || H <- L ];
variation(N, L) ->
[[H | T] || H <- L, T <- variation(N - 1, L), check([H|T]) ].


This solution will only return those lists, that doesn't fail the condition (it works, but really slow for big input).



If [1,1] fails, it will try to generate [1,1,1,2], but those will fail the check as well. I would need a solution, which doesn't try to generate lists that begin with [1,1,...] (or with a previously failing sublist).







erlang






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 15:24

























asked Nov 11 at 15:02









Goba

305




305











  • Can you run the check only on H? Like this… variation(1, L) -> [ [H] || H <- L ]; variation(N, L) -> [[H | T] || H <- L, check(H), T <- variation(N - 1, L) ].
    – Brujo Benavides
    Nov 12 at 11:21











  • I’ll add it as an answer, just for the code formatting :S
    – Brujo Benavides
    Nov 12 at 11:22
















  • Can you run the check only on H? Like this… variation(1, L) -> [ [H] || H <- L ]; variation(N, L) -> [[H | T] || H <- L, check(H), T <- variation(N - 1, L) ].
    – Brujo Benavides
    Nov 12 at 11:21











  • I’ll add it as an answer, just for the code formatting :S
    – Brujo Benavides
    Nov 12 at 11:22















Can you run the check only on H? Like this… variation(1, L) -> [ [H] || H <- L ]; variation(N, L) -> [[H | T] || H <- L, check(H), T <- variation(N - 1, L) ].
– Brujo Benavides
Nov 12 at 11:21





Can you run the check only on H? Like this… variation(1, L) -> [ [H] || H <- L ]; variation(N, L) -> [[H | T] || H <- L, check(H), T <- variation(N - 1, L) ].
– Brujo Benavides
Nov 12 at 11:21













I’ll add it as an answer, just for the code formatting :S
– Brujo Benavides
Nov 12 at 11:22




I’ll add it as an answer, just for the code formatting :S
– Brujo Benavides
Nov 12 at 11:22












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










One small detail first: According to your question, variations(3, [1,2,3]). should generate [[1,1,1,1], [1,1,1,2], …] but it actually generates [[1,1,1], [1,1,2], …]. I will assume the code was right and you meant to say that variations(4, [1,2,3]). should generate [[1,1,1,1], [1,1,1,2], …]



I wrote an alternative version of your function that, using a different order on the right side of the LC, avoids generating list when their prefix is already false when checked with check/1:



variation(1, L) ->
[ [Elem] || Elem <- L ];
variation(N, L) ->
[ Init ++ [Last] || Init <- variation(N-1, L), check(Init), Last <- L].


As you can see, since check(Init) happens before Last <- L, Last is only generated if check(Init) == true.
That will likely have the effect you were looking for.



But… be careful. I'm using ++ in the left side of the LC. You should definitely benchmark your code and see if that has an impact on performance or not.



If it does, and only if it does, you might want to consider using something like this:



variation3(1, L) ->
[ [Elem] || Elem <- L ];
variation3(N, L) ->
[ lists:reverse([Last|lists:reverse(Init)]) || Init <- variation2(N-1, L), check(Init), Last <- L].


Maybe worth it, maybe not… you will need to benchmark your stuff to figure that out.






share|improve this answer






















  • It won't work for me I need to check [H|T]. Unfortunately, the head check is not enough. I need to check the sublists: So if [1] sublist fails, it should not generate lists that begin with 1. I need this for performance reason.
    – Goba
    Nov 12 at 18:08










  • Or another solution would be to generate variations without list comprehension. So it would generate 1 variation, and check it. After this, it would give another variation etc... But list comprehension will load every source lists, so for HUGE inputs, we will run out of memory. But I don't know how to do this without list comprehension
    – Goba
    Nov 12 at 19:37










  • OK. I'll amend my answer.
    – Brujo Benavides
    Nov 13 at 19:45










  • done, @Goba… let me know if that works.
    – Brujo Benavides
    Nov 13 at 20:14










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%2f53249999%2flist-comprehension-stop-generating-certain-lists-if-sublist-fails-on-check%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










One small detail first: According to your question, variations(3, [1,2,3]). should generate [[1,1,1,1], [1,1,1,2], …] but it actually generates [[1,1,1], [1,1,2], …]. I will assume the code was right and you meant to say that variations(4, [1,2,3]). should generate [[1,1,1,1], [1,1,1,2], …]



I wrote an alternative version of your function that, using a different order on the right side of the LC, avoids generating list when their prefix is already false when checked with check/1:



variation(1, L) ->
[ [Elem] || Elem <- L ];
variation(N, L) ->
[ Init ++ [Last] || Init <- variation(N-1, L), check(Init), Last <- L].


As you can see, since check(Init) happens before Last <- L, Last is only generated if check(Init) == true.
That will likely have the effect you were looking for.



But… be careful. I'm using ++ in the left side of the LC. You should definitely benchmark your code and see if that has an impact on performance or not.



If it does, and only if it does, you might want to consider using something like this:



variation3(1, L) ->
[ [Elem] || Elem <- L ];
variation3(N, L) ->
[ lists:reverse([Last|lists:reverse(Init)]) || Init <- variation2(N-1, L), check(Init), Last <- L].


Maybe worth it, maybe not… you will need to benchmark your stuff to figure that out.






share|improve this answer






















  • It won't work for me I need to check [H|T]. Unfortunately, the head check is not enough. I need to check the sublists: So if [1] sublist fails, it should not generate lists that begin with 1. I need this for performance reason.
    – Goba
    Nov 12 at 18:08










  • Or another solution would be to generate variations without list comprehension. So it would generate 1 variation, and check it. After this, it would give another variation etc... But list comprehension will load every source lists, so for HUGE inputs, we will run out of memory. But I don't know how to do this without list comprehension
    – Goba
    Nov 12 at 19:37










  • OK. I'll amend my answer.
    – Brujo Benavides
    Nov 13 at 19:45










  • done, @Goba… let me know if that works.
    – Brujo Benavides
    Nov 13 at 20:14














up vote
1
down vote



accepted










One small detail first: According to your question, variations(3, [1,2,3]). should generate [[1,1,1,1], [1,1,1,2], …] but it actually generates [[1,1,1], [1,1,2], …]. I will assume the code was right and you meant to say that variations(4, [1,2,3]). should generate [[1,1,1,1], [1,1,1,2], …]



I wrote an alternative version of your function that, using a different order on the right side of the LC, avoids generating list when their prefix is already false when checked with check/1:



variation(1, L) ->
[ [Elem] || Elem <- L ];
variation(N, L) ->
[ Init ++ [Last] || Init <- variation(N-1, L), check(Init), Last <- L].


As you can see, since check(Init) happens before Last <- L, Last is only generated if check(Init) == true.
That will likely have the effect you were looking for.



But… be careful. I'm using ++ in the left side of the LC. You should definitely benchmark your code and see if that has an impact on performance or not.



If it does, and only if it does, you might want to consider using something like this:



variation3(1, L) ->
[ [Elem] || Elem <- L ];
variation3(N, L) ->
[ lists:reverse([Last|lists:reverse(Init)]) || Init <- variation2(N-1, L), check(Init), Last <- L].


Maybe worth it, maybe not… you will need to benchmark your stuff to figure that out.






share|improve this answer






















  • It won't work for me I need to check [H|T]. Unfortunately, the head check is not enough. I need to check the sublists: So if [1] sublist fails, it should not generate lists that begin with 1. I need this for performance reason.
    – Goba
    Nov 12 at 18:08










  • Or another solution would be to generate variations without list comprehension. So it would generate 1 variation, and check it. After this, it would give another variation etc... But list comprehension will load every source lists, so for HUGE inputs, we will run out of memory. But I don't know how to do this without list comprehension
    – Goba
    Nov 12 at 19:37










  • OK. I'll amend my answer.
    – Brujo Benavides
    Nov 13 at 19:45










  • done, @Goba… let me know if that works.
    – Brujo Benavides
    Nov 13 at 20:14












up vote
1
down vote



accepted







up vote
1
down vote



accepted






One small detail first: According to your question, variations(3, [1,2,3]). should generate [[1,1,1,1], [1,1,1,2], …] but it actually generates [[1,1,1], [1,1,2], …]. I will assume the code was right and you meant to say that variations(4, [1,2,3]). should generate [[1,1,1,1], [1,1,1,2], …]



I wrote an alternative version of your function that, using a different order on the right side of the LC, avoids generating list when their prefix is already false when checked with check/1:



variation(1, L) ->
[ [Elem] || Elem <- L ];
variation(N, L) ->
[ Init ++ [Last] || Init <- variation(N-1, L), check(Init), Last <- L].


As you can see, since check(Init) happens before Last <- L, Last is only generated if check(Init) == true.
That will likely have the effect you were looking for.



But… be careful. I'm using ++ in the left side of the LC. You should definitely benchmark your code and see if that has an impact on performance or not.



If it does, and only if it does, you might want to consider using something like this:



variation3(1, L) ->
[ [Elem] || Elem <- L ];
variation3(N, L) ->
[ lists:reverse([Last|lists:reverse(Init)]) || Init <- variation2(N-1, L), check(Init), Last <- L].


Maybe worth it, maybe not… you will need to benchmark your stuff to figure that out.






share|improve this answer














One small detail first: According to your question, variations(3, [1,2,3]). should generate [[1,1,1,1], [1,1,1,2], …] but it actually generates [[1,1,1], [1,1,2], …]. I will assume the code was right and you meant to say that variations(4, [1,2,3]). should generate [[1,1,1,1], [1,1,1,2], …]



I wrote an alternative version of your function that, using a different order on the right side of the LC, avoids generating list when their prefix is already false when checked with check/1:



variation(1, L) ->
[ [Elem] || Elem <- L ];
variation(N, L) ->
[ Init ++ [Last] || Init <- variation(N-1, L), check(Init), Last <- L].


As you can see, since check(Init) happens before Last <- L, Last is only generated if check(Init) == true.
That will likely have the effect you were looking for.



But… be careful. I'm using ++ in the left side of the LC. You should definitely benchmark your code and see if that has an impact on performance or not.



If it does, and only if it does, you might want to consider using something like this:



variation3(1, L) ->
[ [Elem] || Elem <- L ];
variation3(N, L) ->
[ lists:reverse([Last|lists:reverse(Init)]) || Init <- variation2(N-1, L), check(Init), Last <- L].


Maybe worth it, maybe not… you will need to benchmark your stuff to figure that out.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 at 20:14

























answered Nov 12 at 11:24









Brujo Benavides

63237




63237











  • It won't work for me I need to check [H|T]. Unfortunately, the head check is not enough. I need to check the sublists: So if [1] sublist fails, it should not generate lists that begin with 1. I need this for performance reason.
    – Goba
    Nov 12 at 18:08










  • Or another solution would be to generate variations without list comprehension. So it would generate 1 variation, and check it. After this, it would give another variation etc... But list comprehension will load every source lists, so for HUGE inputs, we will run out of memory. But I don't know how to do this without list comprehension
    – Goba
    Nov 12 at 19:37










  • OK. I'll amend my answer.
    – Brujo Benavides
    Nov 13 at 19:45










  • done, @Goba… let me know if that works.
    – Brujo Benavides
    Nov 13 at 20:14
















  • It won't work for me I need to check [H|T]. Unfortunately, the head check is not enough. I need to check the sublists: So if [1] sublist fails, it should not generate lists that begin with 1. I need this for performance reason.
    – Goba
    Nov 12 at 18:08










  • Or another solution would be to generate variations without list comprehension. So it would generate 1 variation, and check it. After this, it would give another variation etc... But list comprehension will load every source lists, so for HUGE inputs, we will run out of memory. But I don't know how to do this without list comprehension
    – Goba
    Nov 12 at 19:37










  • OK. I'll amend my answer.
    – Brujo Benavides
    Nov 13 at 19:45










  • done, @Goba… let me know if that works.
    – Brujo Benavides
    Nov 13 at 20:14















It won't work for me I need to check [H|T]. Unfortunately, the head check is not enough. I need to check the sublists: So if [1] sublist fails, it should not generate lists that begin with 1. I need this for performance reason.
– Goba
Nov 12 at 18:08




It won't work for me I need to check [H|T]. Unfortunately, the head check is not enough. I need to check the sublists: So if [1] sublist fails, it should not generate lists that begin with 1. I need this for performance reason.
– Goba
Nov 12 at 18:08












Or another solution would be to generate variations without list comprehension. So it would generate 1 variation, and check it. After this, it would give another variation etc... But list comprehension will load every source lists, so for HUGE inputs, we will run out of memory. But I don't know how to do this without list comprehension
– Goba
Nov 12 at 19:37




Or another solution would be to generate variations without list comprehension. So it would generate 1 variation, and check it. After this, it would give another variation etc... But list comprehension will load every source lists, so for HUGE inputs, we will run out of memory. But I don't know how to do this without list comprehension
– Goba
Nov 12 at 19:37












OK. I'll amend my answer.
– Brujo Benavides
Nov 13 at 19:45




OK. I'll amend my answer.
– Brujo Benavides
Nov 13 at 19:45












done, @Goba… let me know if that works.
– Brujo Benavides
Nov 13 at 20:14




done, @Goba… let me know if that works.
– Brujo Benavides
Nov 13 at 20:14

















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%2f53249999%2flist-comprehension-stop-generating-certain-lists-if-sublist-fails-on-check%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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto