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).
erlang
add a comment |
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).
erlang
Can you run the check only onH
? 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
add a comment |
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).
erlang
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
erlang
edited Nov 11 at 15:24
asked Nov 11 at 15:02
Goba
305
305
Can you run the check only onH
? 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
add a comment |
Can you run the check only onH
? 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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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