Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Rich H.J for C programmers.2006.pdf
Скачиваний:
18
Добавлен:
23.08.2013
Размер:
1.79 Mб
Скачать

Tie and Agenda (switch)

The Tie Conjunction u`v u`n m`v m`n

The backquote character ` is the conjunction named Tie. ` is one of the few conjunctions that produce a noun, so it is neither monadic or dyadic. If an operand of ` is a verb, it is converted to its atomic representation which is a noun form from which the verb can be recovered; then the two operands m and n (both nouns now since any verb was converted to a noun) are joined by executing m,n . So, the result of ` applied between the members of a sequence of verbs is a list of special nouns, each of which is the atomic representation of a verb. We are not concerned with the format of the atomic representation, nor will we create or modify an atomic representation (that's AdvancedGuru work); we will be content to use the values produced by ` . An example is:

+`-`*`%`(+/) +-+-+-+-+-------+ |+|-|*|%|+-+---+|

| | | | ||/|+-+||

| | | | || ||+|||

| | | | || |+-+||

| | | | |+-+---+| +-+-+-+-+-------+

What makes the result of ` special is not the boxing, but the fact that what's in the boxes is not just any old data, but data in the format that can be used to recover the original verbs. Once created, the result of ` can be operated on like any other noun:

a =. +:`-`*`%`(+/) 3 { a

+-+ |%| +-+

0 0 1 0 1 # a +-+-------+ |*|+-+---+|

| ||/|+-+||

| || ||+|||

| || |+-+|| | |+-+---+| +-+-------+

In English grammar, a gerund is a form of a verb that is used as a noun, for example the word cooking in Cooking is fun. The result of ` in J is also called a gerund, and we can see that the name is apt: a gerund in J is a set of J verbs put into a form that can be used as a J noun. It has the latent power of the verbs put into a portable form, like nitroglycerine that has been stabilized by kieselguhr to become dynamite. The blasting cap that sets it off is

99

The Agenda (switch) conjunction m@.v

m@.v (either monad or dyad) uses the result of v to select a verb from the list of verbs m, and then executes that verb.

m@.v requires that m be a valid gerund. It produces a verb which can be used monadically or dyadically and whose ranks are the ranks of v . The operation of this verb is as follows: v y (if monadic) or x v y (if dyadic) is evaluated; it must produce a scalar result r that is a valid index into m; i. e. (-#m) <: r and r < #m . Then, item r{m is selected—it is the atomic representation of one of the verbs that went into m—and that atomic representation is converted to a verb u . Finally, u y (if monadic) or x u y (if dyadic) is executed, and its result is the result of the execution of m@.v .

So, verb0`verb1`verb2 @. v y evaluates v y, resulting in r, and then executes verbr y . The dyadic case x verb0`verb1`verb2 @. v y evaluates x v y, resulting in r, and then executes x verbr y . The verbs may be any valid verb: a primitive, a compound verb, or a named verb.

Examples:

(1&+)`(-&2)@.(2&|) "0 i. 6 1 _1 3 1 5 3

This added 1 to each even number and subtracted 2 from each odd number. Note that we had to assign rank 0 to the overall combined verb, because otherwise the rank of (1&+)`(-&2)@.(2&|) would have been the rank of 2&| which is infinite because m&v has infinite rank.

_5 _3 _1 1 3 5 +`-@.(0&>@:["0) 2 _7 _5 _3 3 5 7

Subtract 2 from elements of x that are negative, add 2 to elements that are nonnegative. Here we assigned the rank to the selector verb in m@.v; that rank was then inherited by m@.v .

5 uname`+`] @. (*@:]"0) _5 0 5

(Remember that monad * is the signum function returning _1 for negative, 0 for zero, and 1 for positive operands) For each atom of y, execute 5 uname y if y is zero,

5 + y if y is positive, and pass y through unchanged (5 ] y) if y is negative. uname must be defined elsewhere. This expression makes use of negative indexing: if * y is negative, verb number _1 (the last item) is taken from the gerund.

m@.v obviously can be used with a small rank to afford great control over what operation is performed cell-by-cell, but if you do that it will have to apply J verbs on small operands, which is inefficient. After all we've been through, I feel confident that I can trust you not to use m@.v with small rank unless it's absolutely necessary.

100