Actually you should break the function down first:

A loop has a few parts:

  1. the header, and processing before the loop. May declare some new variables

  2. the condition, when to stop the loop.

  3. the actual loop body. It changes some of the header's variables and/or the parameters passed in.

  4. the tail; what happens after the loop and return result.

Or to write it out:

foo_iterative(params){
    header
    while(condition){
        loop_body
    }
    return tail
}

Using these blocks to make a recursive call is pretty straightforward:

foo_recursive(params){
    header
    return foo_recursion(params, header_vars)
}

foo_recursion(params, header_vars){
    if(!condition){
        return tail
    }

    loop_body
    return foo_recursion(params, modified_header_vars)
}

Et voilà; a tail recursive version of any loop. breaks and continues in the loop body will still have to be replaced with return tail and return foo_recursion(params, modified_header_vars) as needed but that is simple enough.


Going the other way is more complicated; in part because there can be multiple recursive calls. This means that each time we pop a stack frame there can be multiple places where we need to continue. Also there may be variables that we need to save across the recursive call and the original parameters of the call.

We can use a switch to work around that:

bar_recurse(params){
    if(baseCase){
        finalize
        return
    }
    body1
    bar_recurse(mod_params)
    body2
    bar_recurse(mod_params)
    body3
}


bar_iterative(params){
    stack.push({init, params})

    while(!stack.empty){
        stackFrame = stack.pop()

        switch(stackFrame.resumPoint){
        case init:
            if(baseCase){
                finalize
                break;
            }
            body1
            stack.push({resum1, params, variables})
            stack.push({init, modified_params})
            break;
        case resum1:
            body2
            stack.push({resum2, params, variables})
            stack.push({init, modified_params})
            break;
        case resum2:
            body3
            break;
        }
    }
}
Answer from ratchet freak on Stack Exchange
🌐
Refactoring
refactoring.com › catalog › replaceIterationWithRecursion.html
Replace Iteration with Recursion - Refactoring
This code has a pretty horrible loop: fortunately, we know what it does: unsigned foo (...) { ... unsigned a = f(); unsigned b = g(); ... unsigned c = greatest_common_divisor(a, b); ... } unsigned greatest_common_divisor (unsigned a, unsigned b) { while (a != b) { if (a > b) { a -= b; } else if (b > a) { b -= a; } } return a; } I can now compile and test this. Now that I've extracted the loop (which may, itself, be a sufficient refactoring), I can change it to a recursive form:
Top answer
1 of 2
49

Actually you should break the function down first:

A loop has a few parts:

  1. the header, and processing before the loop. May declare some new variables

  2. the condition, when to stop the loop.

  3. the actual loop body. It changes some of the header's variables and/or the parameters passed in.

  4. the tail; what happens after the loop and return result.

Or to write it out:

foo_iterative(params){
    header
    while(condition){
        loop_body
    }
    return tail
}

Using these blocks to make a recursive call is pretty straightforward:

foo_recursive(params){
    header
    return foo_recursion(params, header_vars)
}

foo_recursion(params, header_vars){
    if(!condition){
        return tail
    }

    loop_body
    return foo_recursion(params, modified_header_vars)
}

Et voilà; a tail recursive version of any loop. breaks and continues in the loop body will still have to be replaced with return tail and return foo_recursion(params, modified_header_vars) as needed but that is simple enough.


Going the other way is more complicated; in part because there can be multiple recursive calls. This means that each time we pop a stack frame there can be multiple places where we need to continue. Also there may be variables that we need to save across the recursive call and the original parameters of the call.

We can use a switch to work around that:

bar_recurse(params){
    if(baseCase){
        finalize
        return
    }
    body1
    bar_recurse(mod_params)
    body2
    bar_recurse(mod_params)
    body3
}


bar_iterative(params){
    stack.push({init, params})

    while(!stack.empty){
        stackFrame = stack.pop()

        switch(stackFrame.resumPoint){
        case init:
            if(baseCase){
                finalize
                break;
            }
            body1
            stack.push({resum1, params, variables})
            stack.push({init, modified_params})
            break;
        case resum1:
            body2
            stack.push({resum2, params, variables})
            stack.push({init, modified_params})
            break;
        case resum2:
            body3
            break;
        }
    }
}
2 of 2
2

Following up on @ratchet freak's answer, I created this example of how the Fibonacci function can be rewritten to a while loop in Java. Note that There's a much simpler (and efficient) way to rewrite the Fibonacci with a while loop though.

class CallContext { //this class is similar to the stack frame

    Object[] args;

    List<Object> vars = new LinkedList<>();

    int resumePoint = 0;

    public CallContext(Object[] args) {
        this.args = args;
    }

}


static int fibonacci(int fibNumber) {
    Deque<CallContext> callStack = new LinkedList<>();
    callStack.add(new CallContext(new Object[]{fibNumber}));
    Object lastReturn = null; //value of last object returned (when stack frame was dropped)
    while (!callStack.isEmpty()) {
        CallContext callContext = callStack.peekLast();
        Object[] args = callContext.args;
        //actual logic starts here
        int arg = (int) args[0];
        if (arg == 0 || arg == 1) {
            lastReturn = arg;
            callStack.removeLast();
        } else {
            switch (callContext.resumePoint) {
                case 0: //calculate fib(n-1)
                    callStack.add(new CallContext(new Object[]{arg - 1}));
                    callContext.resumePoint++;
                    break;
                case 1: //calculate fib(n-2)
                    callContext.vars.add(lastReturn); //fib1
                    callStack.add(new CallContext(new Object[]{arg - 2}));
                    callContext.resumePoint++;
                    break;
                case 2: // fib(n-1) + fib(n-2)
                    callContext.vars.add(lastReturn); //fib2
                    lastReturn = (int) callContext.vars.get(0) + (int) callContext.vars.get(1);
                    callStack.removeLast();
                    break;
            }
        }
    }
    return (int) lastReturn;
}
Discussions

replace for loop with recursive function for
i am working on simple js function... can u tell me how to replace the loop with recursive function.. providing my code below in the fiddle.. I am trying to learn js http://jsfiddle.net/utbsgnzu... More on stackoverflow.com
🌐 stackoverflow.com
May 25, 2017
Explanation for the given example for Replace Loops with Recursion
Tell us what’s happening: Describe your issue in detail here. Unfortunately, my issue is with the lesson itself, and how it’s very unclear and inadequate, especially for a concept that is a bit mind-boggling to come across for the first time (A function that calls itself???). First line: ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
1
July 4, 2022
freeCodeCamp Challenge Guide: Replace Loops using Recursion - Guide - The freeCodeCamp Forum
Replace Loops using Recursion Hints Hint 1: When n More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
130
February 22, 2020
Replace loop using recursion
Tell us what’s happening: I think the code is buggy, since i am using recursion and it is still asking me to use recursoin in order to get the lesson. by the way thanks to the creators of this site for including extra lessons. Your code so far js function sum(arr, n) { // Only change code ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
1
October 18, 2019
Top answer
1 of 4
6

If you can accomplish a task without recursion its a good idea to solve it that way. If you would like to learn about recursion check out some problems like factorial or Fibonacci. These also have iterative solutions but lend themselves much more to recursion than the problem you have here. In this case it is very clear what your algorithm is doing and recursion would make it needlessly harder to understand. Here is one improvement you could make however

for (int e = 0; e < length; e++)
{
    key[4] = letters[e];
    for (int d = 0; d < length; d++)
    {
        key[3] = letters[d];
        for (int c = 0; c < length; c++)
        {
            key[2] = letters[c];
            for (int b = 0; b < length; b++)
            {
                key[1] = letters[b];
                for (int a = 1; a < length; a++)
                {
                    key[0] = letters[a];

                    if (strcmp(crypt(key, salt), hash) == 0)
                    {
                        printf("%s\n", key);
                        return 0;
                    }
                }
            }
        }
    }
} 
2 of 4
2

Although I don't disagree with everyone who has discouraged you from using recursion in this example, I wanted to write it recursively since I think it's a reasonable question.

Here's my attempt at writing it recursively. By doing this, I only need to write the loop once, since the outer loop is handled by the recursion. I've taken some liberties so it isn't exactly equivalent to your code, but I think in principle it is the same (testing all the combinations against hash) and shows the basic idea of how you could write this recursively. I'll assume you have a way of knowing the strcmp check is safe.

int recur(int cur, int klength, char *key, char *letters, int length, char *salt, char *hash)
{
    if (cur == klength)
    {
        if (strcmp(crypt(key, salt), hash))
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        for (int i = 0; i < length; i++)
        {
            key[cur] = letters[i];
            int j = recur(cur+1, klength, key, letters, length, salt, hash);
            if (!j)
            {
                return 0;
            }
        }
        return 1;
    }
}

I would then call this with

recur(5, 0, ...)

to do the 5 loops you wrote. This isn't very elegant, but I think it's clear why this might be more elegant if you expanded your key to require 10 loops (and why it would be terrible for the stack at 10000 loops).

Having said that, my first thought looking at your code wasn't "recursion" it was "those outer loops look pretty similar, so I'd like to get rid of some of them." My code below isn't pretty (hey, it's late at night!), but I think in principle this would be a better approach if you think you might need to increase the number of characters you're testing to 10 (or 10000). What I'm trying to do is maintain an integer equivalent to key in idx. If I increment idx[0] and it is == length I know I need to reset idx[0] = 0 and try incrementing idx[1], etc. Every time I change idx[i] I make an equivalent change to key[i]. Every time I have a new permutation of idx/key, I do your strcmp test to see if I've found the correct one.

int ksize = 5;
int idx[ksize];
for (int i = 0; i < ksize; ++i)
{
    idx[i] = 0;
    key[i] = letters[0];
}
for (int done = 0; !done; )
{
    if (strcmp(crypt(key, salt), hash) == 0)
    {
        printf("%s\n", key);
        return 0;
    }
    for (int i = 0; ; i++)
    {
        if (++idx[i] == length)
        {
            idx[i] = 0;
        }
        key[i] = letters[idx[i]];
        if (idx[i]) // We incremented idx[i] and it wasn't reset to 0, so this is a new combination to try
        {
            break;
        }
        else if (i == ksize-1) // We incremented idx[ksize-1] and it was reset to 0, so we've tried all possibilities without returning
        {
            done++;
            break;
        }
    }
}
🌐
YouTube
youtube.com › watch
Replace Loops using Recursion - Free Code Camp - YouTube
This is a basic JavaScript tutorial where we replace loops using recursion. Recursion is quite difficult for me to get my head around in general.
Published   November 2, 2019
🌐
GitHub
gist.github.com › sanikamal › 488f895ab6be5b9692a893ef12cf57c9
Basic JavaScript: Replace Loops using Recursion · GitHub
Basic JavaScript: Replace Loops using Recursion. GitHub Gist: instantly share code, notes, and snippets.
🌐
Connorocampo
connorocampo.dev › blog › replace-loops-using-recursion
Replace Loops using Recursion (Line-by-line Solution Explanation) | Connor Ocampo's Website
June 3, 2020 - This is a line-by-line code explanation of freeCodeCamp’s Replace Loops using Recursion module in their Basic JavaScript section.
Find elsewhere
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Explanation for the given example for Replace Loops with Recursion
July 4, 2022 - Tell us what’s happening: Describe your issue in detail here. Unfortunately, my issue is with the lesson itself, and how it’s very unclear and inadequate, especially for a concept that is a bit mind-boggling to come across for the first time (A function that calls itself???). First line: “Recursion is the concept that a function can be expressed in terms of itself.” This very first line is already a bit iffy.
🌐
freeCodeCamp
forum.freecodecamp.org › guide
freeCodeCamp Challenge Guide: Replace Loops using Recursion - Guide - The freeCodeCamp Forum
February 22, 2020 - Replace Loops using Recursion Hints Hint 1: When n <= 0 sum(arr, n) returns 0. Hint 2: When n is larger than 0 sum(arr, n) returns sum(arr, n - 1) + arr[n - 1] Solutions: (Click to reveal) function sum(arr, n) { if(…
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Replace Loops Using Recursion Clarification - JavaScript
February 7, 2020 - Tell us what’s happening: I understand how basic function recursion works, the function is simply re-called within the function I understand that the function is calling itself and only when n <= 0 is false, then e…
🌐
Hashnode
hashnode.com › post › fcc-basic-javascript-replace-loops-using-recursion-ck9uevoq1046v7bs1gsbmop2t
fCC - Basic JavaScript: Replace Loops using Recursion - Hashnode
May 5, 2020 - However, notice that multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]. That means you can rewrite multiply in terms of itself and never need to use a loop. function multiply(arr, n) { if (n <= 0) { return 1; } else { return multiply(arr, n - 1) * arr[n - 1]; } } The recursive version of multiply breaks down like this. In the base case, where n <= 0, it returns 1. For larger values of n, it calls itself, but with n - 1.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Replacing loops with recursion
March 9, 2020 - Cant seem to pass recursion test cant get second call to equal 2 can anyone help ? Your code so far function sum(arr, n) { // Only change code below this line if (n<=1){ return 0; } else{ return sum (arr, n - 1) + arr…
🌐
Refactoring
refactoring.com › catalog › replaceRecursionWithIteration.html
Replace Recursion with Iteration - Refactoring
Every Recursion must have a defined base case. In addition, each recursive call must make a progress towards the base case (otherwise recursive calls would be performed infinitely). In our example the base case is n == 0. Implement a loop that will iterate until the base case is reached.
🌐
Quora
quora.com › How-do-I-replace-recursion-with-iteration
How to replace recursion with iteration - Quora
December 17, 2018 - Answer (1 of 3): For any recursive method, we can remove the recursion by writing the code that essentially does what the computer does. The computer uses a stack to keep track of the calls and returns.
Top answer
1 of 4
1

Assuming you have your list set up so that tab[0] is the list of the first strings to print, tab[1] the following strings, etc., you can use recursion as follows:

void print_rec(List<String>[] tab, int depth, String str) {
    int maxdepth = tab.length - 1;
    for (int i = 0; i < tab[depth].size(); i++) { // number of strings at this depth
        if (depth == maxdepth) {
            System.out.println(str + tab[depth].get(i)); // print line
            // no more lists to go through, print the line
        } else {
            print_rec(tab, depth + 1, str + tab[depth].get(i) + " ");
            // add next entry at this level to str and recurse
        }
    }
    // done with this depth, go back up one
}

void printAll(List<Strings>[] tab) {
    print_rec(tab, 0, "");
}

This covers all the entries exactly like nested loops.

2 of 4
1

Here is a solution. It's a modification of my answer to your other post: how to use recursion for nested 'for' loops.

public static void iterate(Object[] previousValues, List<Object>[] tabs) {
    if (tabs.length == 0) {
        System.out.println(Arrays.toString(previousValues));
    }
    else {
        final Object[] values = new Object[previousValues.length + 1];
        for (int i = 0; i < previousValues.length; i++) {
            values[i] = previousValues[i];
        }
        final List<Object>[] nextTabs = new List[tabs.length - 1];
        for (int i = 0; i < nextTabs.length; i++) {
            nextTabs[i] = tabs[i + 1];
        }
        for (Object o : tabs[0]) {
            values[values.length - 1] = o;
            iterate(values, nextTabs);
        }
    }
}
public static void iterate(List<Object>[] tabs) {
    iterate(new Object[0], tabs);
}

public static void main(String[] args) {
    final List<String> firstTab = new ArrayList<>();
    firstTab.add("England");
    final List<String> secondTab = new ArrayList<>();
    secondTab.add("London");
    secondTab.add("Liverpool");
    final List<String> thirdTab = new ArrayList<>();
    thirdTab.add("DG300");
    thirdTab.add("SS500");
    iterate(new List[]{firstTab, secondTab, thirdTab});
}
🌐
GitHub
github.com › Manish-Giri › FreeCodeCamp › blob › master › curriculum › challenges › english › 02-javascript-algorithms-and-data-structures › basic-javascript › replace-loops-using-recursion.english.md
FreeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion.english.md at master · Manish-Giri/FreeCodeCamp
However, notice that multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]. That means you can rewrite multiply in terms of itself and never need to use a loop. function multiply(arr, n) { if (n <= 0) { return 1; } else { return multiply(arr, n - 1) * arr[n - 1]; } } The recursive version of multiply breaks down like this. In the base case, where n <= 0, it returns 1. For larger values of n, it calls itself, but with n - 1.
Author   Manish-Giri