GN=Gary Ng, RM=Raul Miller

GN> What would be the lookup result of _1 { f ?

RM>    _1 { f
RM> gives you the last item in f.

GN>  thanks then why would I get 
GN>  0 0 when the lookup index is _1 _1

For the same reason that when you write:

        0 1 2 { f

you'd get the first 3 items of f  .   In short: the dyad  {  maps indices
(x) to items (y)  .  Its inputs are a list of indices and a universe, and
its output is the items of the universe which exist at those indices.
Things like  _1 _1  work because repeated indices are allowed.  At first
blush, this may seem useless, but in practice it's a useful feature (plus
there's no good reason to restrict the definition of { to prohibit repeats).


If you're wondering why _1 _1 doesn't give the bottom-right corner of the
array, like f[0][0] (or maybe f[0;0]) would give the top-left corner of a 2D
array in a C-like language, the answer is: in J, compound
(multi-dimensional) indices are addressed with boxes.

There are many ways to present the rationale for this decision, but an easy
one is: conceptually, a compound index is a single, atomic thing, yet has
structure (this seems contradictory until you work through some examples,
e.g. "the bottom-right corner" of a 2D array).  In J, atoms with (arbitrary)
structure are represented with boxes.  Voila.

GN>  if I just type -1 { f, I get something else 
GN>  [compared to _1 { f].

In J, _1 and -1 are very different things.  The rule of thumb is, where you
would write -1 in another language, you would write _1 in J, and where you
would write  negate(numeric_argument)  in another language, you'd write
-numeric_argument  in J.

That is, - is a function, applied to a (numeric) argument, whereas _1 is how
you spell the name of the first negative integer (i.e. the _ is an
inseparable part of the name of the constant).  

In more formal, and perhaps illuminating, terms: _1 is one word, and -1 is
two.  Compare  ;:'_1'  with  ;:'-1'  .  For a longer treatment of this
topic, see [1]  .

-Dan

[1]  Forum thread "why is _5+_4 not the same as -5 + -4 ?":
     http://www.jsoftware.com/pipermail/general/2007-April/029806.html 


----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to