C-Style indexing in Forth: Questions and answers


> For consistency reasons,  I think it would be better to
> slightly rename your suggested operators as follows:
>
> []      ( n a-addr1 -- a-addr2 ) "brackets"
> []@     ( n a-addr -- x )        "brackets-fetch"
> []!     ( x n a-addr -- )        "brackets-store"

If you try to use these words in real code, you'll find that
the word []@ is used several times more frequently than other 
words.

Let us compare:

    : xxx ( addr1 addr2 n -- )
        0 ?do  i 2 pick [] .  i over [] .  2 spaces loop ;

vs.

    : xxx ( addr1 addr2 n -- )
        0 ?do  i 2 pick []@ .  i over []@ .  2 spaces loop ;

The symbol @ attracts inadequately much attention. It makes us
think about fetch operations rather than about array elements.

The [] notation is more elegant. What the word "elegance" means
in the world of software? It is adequateness of complexity which
may be seen by the eye.

(This is the same as e.g. temperature measuring: you can use a 
thermometer, get a digit and interpret the digit, or just touch 
with your hand. The second way is often more adequate.)

The word [] is transparent. It looks enough declarative,
so that we understand it as a description rather than a command.

> Moreover, this naming convention is extensible (think about
>
> []C@    ( n a-addr -- b )        "brackets-c-fetch"
>
> and
>
> []C!    ( b n a-addr -- )        "brackets-c-store"
>
> to deal with arrays of characters)

To deal with arrays or characters (that is, with strings),
I use words

C[]  ( n c-addr -- c )
C[]! ( c n c-addr -- )
C[]^ ( n c-addr1 -- c-addr2 )

For dealing with bit arrays I use words

bit[]  ( n addr -- f ) f is non-zero if n-th bit is set
bit[]! ( f n addr -- ) set/reset the n-th bit
BITS   ( n1 -- n2 ) n2 is the minimal number of address 
                    units enough to contain n1 bits


> In fact, the only *needed* operator is [] ( n addr1 -- addr2 )
> Everything else is just in the "poor man optimization
> toolkit" like
>
> 2*
>
> to mean
>
> 2 *


At first, [] is more readable than [] @ . See above and below.
If we eliminate explicit mentioning of array element access
algorithms, let us do this completely.
At second, even as a "poor man optimization toolkit" it is just 
adequate on some embedded systems, e.g. 8051-based.

> With:
>
> []      ( n a-addr1 -- a-addr2 ) "brackets"
>
> you can perform any combination of access.

Such word [] will be in most cases followed by @ . Even if this
makes no difference for the computer, this will be inconvenient
to the programmer. CODE MUST BE LACONIC. CODE MUST BE CONCISE.

This is why VALUEs become popular. They eliminate the need of
mentioning fetch operations explicitly.


< end of "C-Style indexing in Forth: Questions and answers" >