This question already has answers here:
Flatten a list in Prolog
(7 answers)
Closed 7 years ago.
I want to be able to turn
this:
[[[a],b],c]
into this:
[a,b,c]
with a constantly changing lists, so far I've done this:
a([[[A],B],C]).
strechList(List,C):-
List=[H,T],
append(T,List1,C),
strechList(H,List1).
strechList(A,A).
but it doesn't work.
That predicate is called 'flatten'. It's built-in if you're using SWI-Prolog.
Also check this answer.
Related
This question already has answers here:
'Repeat' in Haskell?
(4 answers)
Closed 2 years ago.
Is it possible in Haskell to create list of some element by provided length?
In Python, I can write:
l = [1] * 10
How can I do it in Haskell?
Thanks to Krzysztof Atłasik
replicate 10 1
This question already has answers here:
Does clearing a vector affect its capacity?
(4 answers)
C++ delete vector, objects, free memory
(7 answers)
Closed 3 years ago.
Sometimes I detect in different c++ projects code like this:
std::vector<DataClass> _dataVec;
...
std::vector<DataClass>().swap(_dataVec);
Is this code more effective than obvious and simple clear call
_dataVec.clear();
or these code samples have some kind of difference?
For what purpose I should prefer first variant?
This question already has answers here:
How to choose between map and unordered_map?
(5 answers)
Closed 5 years ago.
According to: This Tutorial
I can't understand the difference between std::map and std::unorderedmap. When and Why we should use Map and Unorderedmap?
As I've read in tutorial you provided, search speed in std::unorderedmap is O(1). While in std::map it's O(log2(n)), where n is size of the map.
So if you have to call std::find often, you can consider this option. While choosing hash function isn't an easy task.
This question already has answers here:
No loop condition in for and while loop
(5 answers)
Closed 7 years ago.
How does an empty for works?
I have seen this code (and works perfectly)
for(;;) {
And I can't comprehend how is this working or why
It is exactly like while(true).
It skips all the conditions so it just empty-executes since in for the expressions are all optional (i.e. you don't have to provide them if you choose not to).
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C++ struct sorting
Is it possible to sort a vector in C++ according to a specified sorting method, like used in Java's Collections.sort that takes a Comparator?
Yes. See the answers to this question from this morning: C++ struct sorting
Yes, it is. Take a look here for an example.