Hi, everyone. I did exercises on basic problems, with the following<div>loop structure:</div><div><br></div><div>for(I, Fp, Fi, Fc) -></div><div> case Fp(I) of</div><div> true -></div><div> Fc(I),</div>
<div> I1 = Fi(I),</div><div> for(I1, Fp, Fi, Fc);</div><div> false -></div><div> ok</div><div> end.<br><div><br clear="all">where I is loop index, Fp, Fi, and Fc are functions for loop predicate,</div>
<div>incremental state, and loop context. Then I can write some normal</div><div>for-loop instructions, such as 9x9 multiple table. (In my country,</div><div>when we're children, we read and remember the 9x9 multiple table,</div>
<div>so it's a famous material for us.) To write a program like</div><div>"for(i=0; i<10; i++) { ... }" in C++, functions are defined as these:</div><div><br></div><div> fp(I) -> I < 10.</div><div>
fi(I) -> I+1.</div><div><br></div><div>It's</div><div> for (i=2; i<10; i++)</div><div> for (j=1; j<10; j++)</div><div> cout << i << " * " << j << " = " << (i*j) << endl;</div>
<div>so fc/1 shell be a loop.</div><div><br></div><div> fc(I) -></div><div> for(I, fun fp/1, fun fi/1, fc1(I)).</div><div><br></div><div> fc1(I) -></div><div> fun(J) -></div><div> io:format("~w x ~w = ~w~n", [I, J, I*J]).</div>
<div> end.</div><div><br></div><div> multiple() -></div><div> for(2, fun fp/1, fun fi/1, fun fc/1).</div><div><br></div><div>Then, I got it work.</div><div><br></div><div>Now I get an image that an index variable in imperative languages</div>
<div>means a sequence of values or states, and then in functional</div><div>languages it's corresponding to a list of values. So when I want</div><div>9x9 table, in C++ I need a variable but in Erlang I need a list with</div>
<div>length 81.</div><div><br></div><div>And, in my imagination, all data in Erlang is put in stack. So, when</div><div>I use 81 cells, they occupy memory with 81 units, right?</div><div><br></div><div>And, in my imagination, memory is free if a function execution ends,</div>
<div>so by using loops above I partially save memory because of using</div><div>nested for/4, right?</div><div><br></div><div>-- </div><div><div><br></div>Best Regards.<div><br></div><div>--- Y-H. H.</div><div><br></div><br>
</div></div>