Actually you should break the function down first:
A loop has a few parts:
the header, and processing before the loop. May declare some new variables
the condition, when to stop the loop.
the actual loop body. It changes some of the header's variables and/or the parameters passed in.
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 ExchangeActually you should break the function down first:
A loop has a few parts:
the header, and processing before the loop. May declare some new variables
the condition, when to stop the loop.
the actual loop body. It changes some of the header's variables and/or the parameters passed in.
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;
}
}
}
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;
}
c - How to Replace For Loops with Recursion - Stack Overflow
freeCodeCamp Challenge Guide: Replace Loops using Recursion - Guide - The freeCodeCamp Forum
Replace loop using recursion
replace for loop with recursive function for
Videos
From logical view it looks fine. You could also try the ternary operator if you want to play around.
return n <= 0 ? 0 : sum(arr, n - 1) + arr[n - 1];
The first block is the if question. If its true is goes to the second block (starts whith ?) and if its false it goes to the third block (starts with :).
I don't think we can make it shorter:
const sum = (arr,n) => --n<0 ? 0 : sum(arr,n) +arr[n]
console.log ( sum([1], 0) )
console.log ( sum([2, 3, 4], 1) )
console.log ( sum([2, 3, 4, 5], 3) )
.as-console-wrapper { max-height: 100% !important; top: 0; }
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;
}
}
}
}
}
}
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;
}
}
}
function operation() {
console.log("testing");
}
function repeat(operation, num) {
if (num === 0) return;
operation();
repeat(operation, num-1);
}
//repeat(operation, 10);
module.exports = repeat
Loops are iterative by nature. Recursive approach does not really fit into this situation. Anyhow, here you go. But use it only for fun, never for real :)
function repeat(func,maxruns,run){
if(run>=maxruns){
return;
}
func();
repeat(func,maxruns,(run||0)+1);
}
repeat(operation,10);
From logical view it looks fine. You could also try the ternary operator if you want to play around.
return n <= 0 ? 0 : sum(arr, n - 1) + arr[n - 1];
The first block is the if question. If its true is goes to the second block (starts whith ?) and if its false it goes to the third block (starts with :).
I don't think we can make it shorter:
const sum = (arr,n) => --n<0 ? 0 : sum(arr,n) +arr[n]
console.log ( sum([1], 0) )
console.log ( sum([2, 3, 4], 1) )
console.log ( sum([2, 3, 4, 5], 3) )
.as-console-wrapper { max-height: 100% !important; top: 0; }
It's possible to replace recursion by iteration plus unbounded memory.
If you only have iteration (say, while loops) and a finite amount of memory, then all you have is a finite automaton. With a finite amount of memory, the computation has a finite number of possible steps, so it's possible to simulate them all with a finite automaton.
Having unbounded memory changes the deal. This unbounded memory can take many forms which turn out to have equivalent expressive power. For example, a Turing machine keeps it simple: there's a single tape, and the computer can only move forward or backward on the tape by one step at a time — but that's enough to do anything that you can do with recursive functions.
A Turing machine can be seen as an idealized model of a computer (finite state machine) with some extra storage that grows on demand. Note that it's crucial that not only there isn't a finite bound on the tape, but even given the input, you can't reliably predict how much tape will be needed. If you could predict (i.e. compute) how much tape is needed from the input, then you could decide whether the computation would halt by calculating the maximum tape size and then treating the whole system, including the now finite tape, as a finite state machine.
Another way to simulate a Turing machine with computers is as follows. Simulate the Turing machine with a computer program that stores the beginning of the tape in memory. If the computation reaches the end of the part of the tape that fits in memory, replace the computer by a bigger computer and run the computation again.
Now suppose that you want to simulate a recursive computation with a computer. The techniques for executing recursive functions are well-known: each function call has a piece of memory, called a stack frame. Crucially, recursive functions can propagate information through multiple calls by passing variables around. In terms of implementation on a computer, that means that a function call might access the stack frame of a (grand-)*parent call.
A computer is a processor — a finite state machine (with a huge number of states, but we're doing computation theory here, so all that matters is that it's finite) — coupled with a finite memory. The microprocessor runs one giant while loop: “while the power is on, read an instruction from memory and execute it”. (Real processors are much more complex than that, but it doesn't affect what they can compute, only how fast and conveniently they do it.) A computer can execute recursive functions with just this while loop to provide iteration, plus the mechanism to access memory, including the ability to increase the size of the memory at will.
If you restrict the recursion to primitive recursion, then you can restrict iteration to bounded iteration. That is, instead of using while loops with an unpredictable running time, you can use for loops where the number of iterations is known at the beginning of the loop¹. The number of iterations might not be known at the beginning of the program: it can itself have been computed by previous loops.
I'm not going to even sketch a proof here, but there is an intuitive relationship between going from primitive recursion to full recursion, and going from for loops to while loops: in both cases, it involves not knowing in advance when you'll stop. With full recursion, this is done with the minimization operator, where you keep going until you find a parameter that satisfies the condition. With while loops, this is done by keeping going until the loop condition is satisfied.
¹ for loops in C-like languages can perform unbounded iteration just like while, it's just a matter of convention to restrict them to bounded iteration. When people talk about “for loops” in theory of computation, that means only loops that count from 1 to $n$ (or equivalent).
Every recursion can be converted to iteration, as witnessed by your CPU, which executes arbitrary programs using a fetch-execute infinite iteration. This is a form of the Böhm-Jacopini theorem. Moreover, many Turing-complete models of computation have no recursion, for example Turing machines and counter machines.
Primitive recursive functions correspond to programs using bounded iteration, that is, you have to specify the number of iterations that a loop is executed in advance. Bounded iteration cannot simulate recursion in general, since the Ackermann function isn't primitive recursive. But unbounded iteration can simulate any partially computable function.