lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello List,

On Wed, Feb 07, 2018 at 09:24:15PM -0600, Coda Highland wrote:
> ...
> Also, it's not FULLY commutative: a[1][2] can be written as 1[a][2],
> but 1[a][2] cannot be written as 1[2][a]. There are limits to the
> magic.

It seems more accurate to say that C subscripting is not associative: To
explain, let us define

  x!y = *(x + y)

Using !, we can write x[y] as x!y.

Once subscripting has been defined as an infix operator, an interesting
question becomes, then, how should x!y!z be interpreted?  Should x!y!z
be (x!y)!z or x!(y!z)? This is important, because these two
interpretations don't lead to the same result: In fact

  (x!y)!z = *(*(x + y) + z)

whereas

  x!(y!z) = *(x + *(y + z))

so easily seen to be different. This is in contrast to infix operators
like + where, mathematically,

  (x + y) + z = x + (y + z)

and the associative law is said to be fulfilled.

So, in summary, the associative law is not fulfilled for !. Or, in other
words, ! is not associative. And, since ! is just a different way of
writing C subscripting, that operation is not associative either.

Rewriting a[1][2] further than 1[a][2] using the fact that C subscripting
is commutative leads to 2[1[a]] and this is definitely still correct.

Note: ! is the BCPL vector application operator, for example as described
in

  Pete Gardner, The BCPL Reference Manual, University of Essex
  Department of Computer Science Centre, 2nd edition, September 1976,
  revised 1977.

See also https://www.bell-labs.com/usr/dmr/www/bcpl.html.

> ...

Best regards
Thorkil