I’m going to use this picture as a reference, so take a look at it, it’s just a simple summation equation without any inputs. http://ww2.tnstate.edu/ganter/BIO311-Ch11-Eq2a.gif
Let’s say I had a simple javascript for loop
for( i=0; i<2 ; i++) {
//do
}
else {
//etc
Now, for loops seem to me to be little bit similar to summation equations.
So if the i=0 is the “initialization”, the i<2 is the “condition”, and the i++ is the “increment”. Could the initialization be compared to the “i=1”, the “condition” compared to the “I”, the “n1” compared to the “increment”, and the n* (f(x)) compared to the “do”?
What would be the appropriate analogies, if any, to these two function types?
* Please feel free to migrate this question to the appropriate forum but I figured I’d first pass it to the real programming forum because I’m talking about for-loops.
EDIT: I think I’m definitely right aboute “initialization” and the “condition”, those are obvious enough. But the rest of it…I’m just not sure about. Maybe the i++ is more like the n*/f(x). I don’t know.
EDIT2: I don’t know whose answer to grant as “correct”…is anybody below explicitly wrong in any way? I’m going to leave this open, please vote on the answer you think is the best. If you can’t decide, like me, vote for all of them and/or give your own answer
You can say
i = 0is the initialization, andi < 2specifies the end point of the range.i++is the implicit increment that the summation operation assumes.Note that not all of mathematical notations are conveniently translatable to code as in the example you gave. Sometimes, you can specify infinity as the end point of the range in mathematics, but you won’t write something like that in code (usually, you will check the difference between iteration and stop when the difference is smaller than a threshold). Sometimes, you can even specify condition of the variable in mathematics (let’s say we only want to sum up those with index divisible to 3), but you won’t be able to specify something like that in javascript.
If you are looking to write code that are close to mathematics, you can have a look at Haskell.