Let’s say we have a function:
generate([],Result) ->
Result;
generate([H|T],Result) ->
case H > 0 of
true -> generate(T,[H*2|Result]);
false -> generate(T,Result)
end.
So, basically the function does something with each H and adds this something to Result.
Now, if I call generate() from another function:
do_generate([],Result) ->
Result;
do_generate([List|Other_lists],Result) ->
do_generate(Other_lists,[generate(List,[])|Result]).
The problem is that I do not want the do_generate() function to add an empty list [] to its result. What if I call do_generate([[0,0],[1,2]],[])? I can end up with something like this: [[2,4],[]].
If I embed do_generate() into other functions I might end up with even more empty lists inside the result list, which is very inconvenient for later use.
How can I avoid adding [] to Result in other way than checking with case generate(List,[]) == [] of each time?
Is there any ‘best practice’ for calling a sequence of deeply nested functions and getting rid of empty lists [] in the final result with ease?
you can get rid of all empty lists from the result, like