M
M
Maxim2020-05-05 12:23:16
JavaScript
Maxim, 2020-05-05 12:23:16

Why can't loops be used in functional programming?

When declaring a loop, we assign a value (let i = 0) and this value cannot be changed from outside.
Each time this function is called with a loop, i in this case will always be 0 on the first iteration.
And as a second option, we can pass an argument to the function that will set the value of the counter i.
So why can't loops be used in functional programming?

Addendum:
My question can be rephrased as follows: does it make rational sense to follow these rules absolutely and without exceptions?

Here is an example:

const i = 666;

function count (a,b) {
  for (let i = 0; i < 10; i++) {
    console.log((a+b)*i);
  }
}
count(1,2);

console.log(i);


This function is pure: it will always produce the same result with the same arguments, and it does not change any variables outside of itself or in the global scope. It turns out a complete rejection of cycles, it's like a commandment: "So the creator said, just follow this rule, don't ask questions."

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Karpion, 2020-05-05
@max_shane

Functional programming assumes that any variable is assigned a value exactly once and cannot be assigned. changed. This condition means the impossibility of completing the cycle, and any: for, while, repeat-until.
Those. the problem is not with the initial value assignment, but with subsequent type changesi++

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question