Vocabulary/zeroco

From J Wiki
Jump to: navigation, search

>> <<   Back to: Vocabulary Thru to: Dictionary

[x] 0: y Constant Function

Rank Infinity -- operates on [x and] y as a whole -- WHY IS THIS IMPORTANT?



The value 0, whatever [x and] y happen to be.

The argument y can therefore be any noun. Ditto x, if present.

   x=: ;:'The quick brown fox is ignored by the lazy dog'
   y=: i. 5 3

   0: y
0
   $$ 0: y
0

   x 0: y
0

Common uses

1. Pad the ends of a numeric vector with zeros.

   pad=: 0: , ] , 0:
   pad i.9
0 0 1 2 3 4 5 6 7 8 0

Note: 0 will work in place of the first 0: here, but not the second. This is because the train of verbs applied to argument y

   (a b c d e) y

is equivalent to the explicit expression:

   (a y) b ((c y) d (e y))

from which you can see that 0: (standing in for a and e here) is called with an unwanted argument y which must be lost.


Related primitives


More information

0: is equivalent to (0"_). Using Rank in this way makes a verb out of a noun (viz. a string or numeric constant).

0: has sister-primitives _: (=infinity), 1:, 2:, 3:, ... 9: and _1:, _2:, _3:, ... _9:, returning the corresponding integers.

NOTE: there is no (__:). Use: (__"_) instead.

Old J code makes more use of 0: (1: 2: etc.) than up-to-date code. You'll often find in old examples that 0 works just as well. This is because a recent version of J defined the train (NOUN VERB VERB) as meaning (NOUN"_ VERB VERB).

This family of primitive verbs is more useful than it first appears, especially when defining a verb tacitly. Tacit definitions don't like embedded variables and constants. They behave best when built from verbs, adverbs and conjunctions.

Trains of primitives form hooks and forks. These receive data from arguments x and y in a complicated way. Use 0: (and family) as a verb, knowing that it will mop up unwanted values presented as arguments and reliably return a scalar 0.


More uses

1. Pascal's Triangle.

   bc =:  < 0&(, + ,~) 1:     NB. binary coeff
   bc 6
1 0  0  0 0 0
1 1  0  0 0 0
1 2  1  0 0 0
1 3  3  1 0 0
1 4  6  4 1 0
1 5 10 10 5 1

This won't work if noun: 1 is used in place of verb: 1:

2. Avoid an accidental vector constant, when what you want to do is make a verb out of a modifier

   (0 1} *:) i.6      NB. Try to amend 1{*: i.6 to zero
|rank error
|       0 1}i.6
   (0: 1} *:) i.6  NB. Avoid adjacent numbers
0 0 2 3 4 5