User:Dan Bron/backtick

From J Wiki
Jump to navigation Jump to search

` (tie)

The primitive ` is a conjunction <link>; it is a convenient way to produce gerunds, usually to feed to other primitives.

Tie takes two arguments, either of which may be a noun or a verb. In the common case where both arguments are verbs, they are converted to their atomic representations (basically, the verbs represented as nouns), and concatenated together, producing a gerund. For more information about gerunds, see the sidebar.

In the general case, verb argument(s) are converted to their atomic representation, and noun argument(s) left untouched. Then the results are concatenated (i.e. the RHA is appended to the LHA), as with ,.

From this definition, we see that ` is merely a higher-order equivalent to ,. Both primitives simply produce the concatentation of their arguments. The only difference between ` and , is that the former can also take verbs as arguments, in which case it will convert them to noun(s) before the concatenation.

Mnemonic: Tie is a higher-order analog of append, and the character ` is visually similar to , but is "higher up".

With either primitive, remember that only conformable types can be concatenated (numbers with numbers, chars with chars, boxes with boxes, symbols with symbols). The atomic representation of a verb (or anything else) is always a box, and so it follows that if one argument to ` is a verb, the other must either be a verb or a boxed noun.

In practice, explicit noun arguments to tie are rare, but treating them is required. To see why, we must recognize that e.g. +`-`* is actually (+`-)`*, and the result of the parenthesized expression is the result of tie, which always produces a gerund, which is a boxed noun. This boxed noun is then then passed as an argument to the rightmost tie, which then concatenates it with the atomic representation of *.


   +`-            NB.  Produce a gerund of + and -
   +`-`*          NB.  Produce a gerund of + and - and * (this works like (+`-)`* )
   *`a:           NB.  Noun arguments are OK too (rarely used)
   *`9            NB.  Careful! Just like with append, arguments must agree in type.
   1`2            NB.  But so long as the types agree, you can use ` just like ,  .
   'hi'`' there'  NB.  Of course, for the noun-noun case, you're better off just using append.
   *`(<9)         NB.  The atomic representation of verbs is always boxed, so you can tie boxes to verbs.
   (<9)`*         NB.  And verbs to boxes, etc.  This can be used for advanced "adverbial" programming.



   foo`bar`baz@.selectionVerb  NB.  Select/case statement (functional style)
   foo`bar`baz`:0              NB.  Multi-application (essentially equivalent to (foo , bar ,: baz) ). See also m`:3, m`:6 .
   foo^:(bar`baz) y            NB.  Equivalent to  foo^:bar baz y  .  Dyad has useful definition too.
   x foo`bar`baz} y            NB.  Tacit merge; equivalent to (x foo y) (x bar y)} x baz y .  Monad has useful definition too.


   foo`bar`baz/ list           NB.  Reduces the list, alternating between foo, bar, and baz (e.g. +`*/i.6 is 0+1*2+3*4+5).  See also /. \ \. and ;.
   foo`bar D. n                NB.  Function foo with an assigned n-th derivative of bar.
   foo`''                      NB.  Convenient, anonymous way to produce the atomic rep of foo.
   foo`bar`baz 6!:2@:,&> ' y'  NB.  Convenient way to time the verbs foo, bar, and baz against the argument y (no parens needed).
   undefined`names`work`fine   NB.  J treats undefined names as proverbs <link>, so ` treats them as verbs and accordingly produces atomic reps.


  1. Tie is not the only way to produce gerunds, though it sees the most use. But anything that can produce an atomic representation can be used to produce a gerund (i.e. an array of atomic representations). The foreign 5!:1 <link> can be useful if you want to produce gerunds of non-verbs (e.g. adverbs or conjunctions -- but even gerunds of nouns are possible).
  2. Tie only ever produces vectors (aka lists aka 1-dimensional arrays); but higher-rank gerunds have their uses, and can be produced from the output of ` in the usual ways (e.g. 2 2 $ +`-`*`%).
  3. The literal character '`' is also used in assignment statements. This is distinct from its role as a primitive (i.e. outside of strings), but serves a mnemonic function. When '`' is the first character of a string left-argument to a copula (either =: or =.) <link>, the (boxed) right argument is evoked as a gerund, before the assignment takes place. For example,
foo =: +
bar =: -
baz =: * 
can be accomplished in a single statement with '`foo bar baz'=:+`-`*. See copulae <link> for more information.

4. The literal character '`' is also used as a sigil to denote the "symbol" data type (symbols <link>, like numbers, characters, and boxes, are a distinct data type, often used as an efficient substitute for boxed strings). This is also distinct from its role as a primitive, for example:
   ] boxed_strings =: ;:'here are some boxed strings'
   ] symbols =: s: boxed_strings
`here `are `some `boxed `strings
   {: boxed_strings
   {: symbols
   s: here`are`some`boxed`strings NB.  Silly implication of the definition of s: and the observation in the last "advanced use case", above.
`here `are `some `boxed `strings
5. The adverb `'' is in a sense the obverse of the more familiar adverb "_. The latter turns any noun into a verb, and leaves verbs (mostly) untouched. The former turns any verb into a noun, and leaves (most) nouns untouched. For this reason, it is quite useful for adverbial programming (as the other is for more familiar verbal programming).

sidebar: gerunds

Gerunds, for the most part, are representations of verbs as nouns. The analogy is to English, where in e.g. "He enjoys swimming", the gerund "swimming" seems like a verb, but is really a noun (and is used as such: the object of a sentence).

Similarly, in J, we may use gerunds to treat verbs as nouns. You can think of a gerund as a collection of "dormant" verbs, each ready to be awakened when the time is right, but until then, they can be conveniently manipulated as arrays, like any other noun.

This is useful, for example, if we want to select different verbs based on certain characteristics of the input. One of the most common uses of gerunds is as the left-argument to the @. conjunction <link>, as in foo`bar`baz@.selectionVerb y, which is equivalent to the select statement <link>
   select. selectionVerb y
       case. 0 do. foo y
       case. 1 do. bar y
       case. 2 do. baz y

Note that the gerund foo`bar`baz allowed us to pass multiple verbs to @. all at once; this was accomplished by making a noun representation of each verb, then assembling these into a list. Thereafter, @. had only to use normal selection <link> mechanisms to pick the appropriate candidate from this array, and apply the elected verb to the argument. See the description of @. for more discussion.

Now, gerunds have subtleties; for example, they can represent more than just verbs (in fact, they can represent any part of speech, including nouns), and they can be multidimensional (this can be useful for the same reasons "regular" multidimensional arrays can be useful). These generalizations also find uses; for example, since verbs can only take nouns as input (and correspondingly only produce nouns as output), gerunds are a convenient way to pass (representations of) non-noun arguments to verbs. And, of course, gerunds are arrays of atomic representations <link>, which themselves have uses (e.g. reflection).

But in practice, gerunds are nearly always simple vectors of verbs produced with ` which are immediately passed as arguments to certain primitive adverbs and conjunctions, which provide useful behavior for gerunds (distinct from their "normal" behavior for verb arguments). In approximate decreasing order of frequency, these are @. (agenda <link>), `: (evoke gerund <link>), } (amend <link>), ^: (power <link>), and sometimes one of ;. / /. \ \. (cut, insert, oblique/key, prefix/infix, suffix/outfix <links>) for their ability to apply gerunds successively (and cyclically) to interesting partitions of the input. Also, one can define specific partial derivatives using gerunds with the conjunction D..

The key thing to remember is that gerunds are just nouns; arrays like any other. Their power lies in the fact that their individual elements can be evoked and applied to arguments (and so the power of gerunds actually lies in the tools that use them). }}