Vocabulary/Variables

From J Wiki
Jump to navigation Jump to search

Back to: Vocabulary

Variables

J does not have "variables" as such. Instead it has the concepts of noun and pronoun, the latter being a name to which a noun happens to be assigned in the current namespace.

This isn't just a matter of J's quirky naming conventions. A pronoun is radically different from a (C) "variable". This difference is masked when using (=.) or (=:) in the J session, or in the body of an explicit verb. But as soon as you come to write your own tacit verb, it becomes clear just how different a pronoun is from a "variable".

C rests heavily on the notion of a "variable", which is essentially the old Assembler notion of a named memory location. The programmer can think of it as a named container into which a value can be placed. That value can be both retrieved and altered at any time. Thus if a given C expression contains the name of the container, e.g. Z, then as soon as the expression is executed, the token Z is replaced with its value, whatever it happens to be at the time.

It follows that Z can serve as a container for the result of a partial computation. This leads to a characteristic style of programming which can't be replicated in tacit J. Nor would you want to.

Z = 10
Z = 2 * (Z = Z+1) + Z

Note that a C "variable" is not at all like a mathematical variable. The latter is the symbol for an arbitrary member of some set. Thus if z is a mathematical variable (a member of the set Z of natural numbers, say), the proposition:

Let z=z+1

cannot be satisfied by a choice of z from Z. In other words the subset of Z to which z refers is empty.

Now, inside the body of an explicitly defined J verb, it is possible to translate the above C statements to J sentences thus:

z =. 10
z =. 2 * z + (z =. z+1)   NB. a different order of execution

The results will be the same.

However what happens in J when these sentences are executed is this

  • The noun (10) is assigned to the name z (which thereby becomes a pronoun)
  • The phrase (z+1) is evaluated and the resulting noun is assigned to the name z (overwriting its existing value)
  • The phrase (2 * z + z) is evaluated and the resulting noun is assigned to the name z.

UNFINISHED


CategoryNuVoc CategoryWorkInProgress