Why does anyone uses I J for loop counter variables names
Just because it’s new 2013 year, I started to learn Forth) language. It’s cool stack-oriented language, with strict KISS principle in design. It’s a shame that it’s completely obsolete and forgotten.
So, I started with a cool book “Starting Forth” (with writing a kindlefodder recipe). A very nice book, btw. Turns out, the only serious and up-to-date open source implementation of ANS Forth gforth has a broken homebrew formula, so I had to fix it
Also, anything around Forth is pretty obsolete - for example, there is couple package managers and no comment - documentation generation system. So plan is to write one (probably it gonna be literate-programming style docco clone with Forth-specific features)
So, what about I
and J
as traditional loop counter variables names?
: table
cr 11 1
do
11 1
do
i j * 5 u.r
loop
cr
loop ;
table
(again, it’s a shame that Pygments support Ada and Befunge, but does not support Forth)
Here we have two loops. Outer loop uses 1st
and 2nd
item of control stack for counter and finish value (1
and 11
initially), inner loop uses 3rd
and 4th
(1
and 11
initially, as well). Word I
in Forth copies 1st
value from control stack on top of stack, J
copies 3rd
value from control stack on top of stack, so they look as loop counters. That’s it! Full KISS! :)
Output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
ok