Vocabulary/Assignment

From J Wiki
Jump to navigation Jump to search

Private and Public Assignment

Assignment

Assignment defines a name, giving it a value. When the name appears in subsequent sentences, the assigned value will be recovered.

Note: the value assigned need not be a noun.

In the example below, the execution of 3 : 0 produces a verb, which is then assigned to the name verb1. After that, verb1 behaves as a verb.

   verb1 =: 3 : 0   NB. Create a verb and assign it to a name. The body of the verb follows:
priv =. y + 1       NB. private assignment
pub =: y            NB. public assignment
priv                NB. priv is defined inside the verb. Here it provides the result of the verb
)

   verb1 3          NB. Execute the verb, giving result of 4
4

   pub              NB. The public name is still visible...
3

   priv             NB. ...but the private one is not
|value error: priv

For new users: J has a hierarchy of namespaces that are searched to find the definition of a name. An executing explicitly-defined entity has a private namespace all to itself; beyond that are other namespaces called locales.

Normal name lookup searches the private namespace first followed by some of the locales, but you can look in a locale directly by using a locative.

You can remain blissfully unaware of all this. If you don't use locatives, all your public assignments and name searches will be in the default locale, and you can focus on the distinction between public and private namespaces.

Simple Assignment

Simple assignment takes the form name =: value (or name =. value). The value may be any of the main parts of speech (noun, verb, adverb, or conjunction). After the assignment, the name takes the part of speech and value of the assigned value.

Any previous definition of the name is superseded by the new value.

In public assignment (as performed by =:), the name is always defined in a locale: in the specified locale if the name is a locative, or in the implied locale otherwise. This differs from private assignment (=.) which may define a name in a private namespace.

Public assignment to a non-locative is not allowed if that name is defined in the active private namespace (because it is almost certainly an error: the name so defined could never be referenced, since it would be hidden by the private name).

Names assigned by public assignment are public in that they can be referenced by a locative from anywhere. If the name's locale is in the search path of the implied locale, the name can be referenced without a locative, by using the simple name.

Names defined in the z locale are in the search path of all locales, and are therefore global: they can be referred to using the simple name from anywhere.

   n =: 5              NB. assign a noun
   n
5
   v =: -              NB. assign a verb
   v
-
   v n                 NB. use the names
_5
   name_loc_ =: 2 3 4  NB. assign in locale loc

Multiple Assignment

Multiple assignment takes the form 'string' =: value (or 'string' =. value).

The string is broken into words, each of which must be a valid name. The names may be a mixture of locatives and non-locatives. Each item of the value is assigned to the corresponding name, with one level of boxing removed.

If there is only one name in the string, the value is not unboxed.

Having the values boxed allows you to assign dissimilar values in one assignment. You can link a number of values using val0 ; val1 ; val2... and then assign them to individual names.

The number of items of value must equal the number of names, unless value is an atom, in which case it is replicated to match the number of names.

   3 ; 4  NB. a noun
+-+-+
|3|4|
+-+-+
   'a b' =: 3 ; 4
   a   NB. Note boxing removed
3
   b
4

Note: This works also

   'a b' =: 3 4
   a
3
   b
4

It works because "removing a level of boxing" from an unboxed list is a valid thing to do (it's a no-op).

If you want to assign the entire list  3 4 to both a and b, then make the list into an atom by boxing it

   'a b' =: < 3 4
   a
3 4
   b
3 4

AR Assignment

AR assignment is a variant of multiple assignment, activated when the first character of string is the back-quote ` . The items of value must be atomic representations (ARs), and each AR is converted to a value before being assigned to the corresponding name.

The easiest way to produce an AR is with Tie u`v which produces the ARs of verbs u and v.

AR assignment is most often used to assign multiple names with verb values:

   +`-`*`%  NB. 4 ARs
+-+-+-+-+
|+|-|*|%|
+-+-+-+-+
   '`plus minus times divide' =:  +`-`*`%  NB. Assign 4 verb-names
   plus
+
   divide
%
   9 divide 2 plus 1
3

Indirect Assignment

Indirect assignment takes the form (nameexpr) =: value. The nameexpr, when evaluated, must produce either a list of boxed names or a string suitable as the string in multiple or AR assignment. The items of value are assigned to the names as in multiple assignment.

   'a' , each '123'   NB. An expression creating three names
+--+--+--+
|a1|a2|a3|
+--+--+--+
   ('a' , each '123') =: 100 101 102  NB. Assignment to the names
   a1
100
   a2
101
   a3
102

CategoryNuVoc CategoryVocAssign CategoryVocValue CategoryVocVariable