Quora Feeds
Active Member
Dragan Nikolov
Sadly enough, most programmer today don’t even understand the concept of recursion rather than looping to perform tasks.
Example:
I want to list all numbers from 1 to 20 into a string like this:
“1, 2, 3, 4, 5… 20”
I could use the tried and true:
function getMyNumbersLoop(int start, int max) {
String current_string = “”;
for (int i = start; i <= max; i++) {
current_string += i + “, “;
}
return current_string;
}
print(getMyNumbers(1, 20));
However…
With the power of recursive functions/methods you can do the exact same thing, only by using MORE code and making the function as a whole far more complicated and difficult to implement/use!
Here’s a great example of the same function written recursively!
function getMyNumbersRecursive(int start, int max, String current_string) {
current_string += start + ", ";
if (start == max) { return current_string; }
return add_numbers(++start, max, current_string);
}
print(add_numbers(1, 20, “”));
One of the simpler ways to do this would be like this using a higher level language like Qt:
QStringList makeMyList(int start, int max) {
QStringList myList;
while (start < max) { start++; myList << QString(“%1”).arg(start); }
return myList;
}
void someCommand() { print(makeMyList(0, 20).join(", ")); }
not only is this faster than pretty much any other way besides throwing it into its own thread, but the code uses extremely fast methods such as QString() constructor and QStringList().join() to create the desired String with very little overhead and even less code/complexity.
See Questions On Quora
Continue reading...
Sadly enough, most programmer today don’t even understand the concept of recursion rather than looping to perform tasks.
Example:
I want to list all numbers from 1 to 20 into a string like this:
“1, 2, 3, 4, 5… 20”
I could use the tried and true:
function getMyNumbersLoop(int start, int max) {
String current_string = “”;
for (int i = start; i <= max; i++) {
current_string += i + “, “;
}
return current_string;
}
print(getMyNumbers(1, 20));
However…
With the power of recursive functions/methods you can do the exact same thing, only by using MORE code and making the function as a whole far more complicated and difficult to implement/use!
Here’s a great example of the same function written recursively!
function getMyNumbersRecursive(int start, int max, String current_string) {
current_string += start + ", ";
if (start == max) { return current_string; }
return add_numbers(++start, max, current_string);
}
print(add_numbers(1, 20, “”));
One of the simpler ways to do this would be like this using a higher level language like Qt:
QStringList makeMyList(int start, int max) {
QStringList myList;
while (start < max) { start++; myList << QString(“%1”).arg(start); }
return myList;
}
void someCommand() { print(makeMyList(0, 20).join(", ")); }
not only is this faster than pretty much any other way besides throwing it into its own thread, but the code uses extremely fast methods such as QString() constructor and QStringList().join() to create the desired String with very little overhead and even less code/complexity.
See Questions On Quora
Continue reading...