I'm not a J expert, but I've looked for some information that might help you.
Well, if you took a look at the documentation, you saw that the word function is not so common in J. Henry Rich, author of J for C programmers , assimilates function to the term verb in J.
As this same author attests, verbs can only receive up to two arguments (or, in the idiom of J, operands). With this, we have two types of verbs, monadics or dyadics, which are given one and two operands respectively.
As a result, J assigns the operands automatically to you. In the case of a monadic verb, the name that will be passed to the verb body sentences will be y
. When calling a verb monadic
, the operand should be to the right of the verb.
To build a monadic verb:
soma1 =: monad define
y + 1
)
Note that you can pass both a single number and an array to the verb that it will work:
soma1 1
2
soma 1 2 3
2 3 4
In dyadic verbs, the names of the operands will be x
and y
. When calling the verb, x
should be on your left and y
should be on your right. To construct a verb dyadic :
somaXproduto =: dyad define
(x + y) * x * y
)
The same remark about the types of operands made for verbs monadics is valid for dyadics :
1 somaXproduto 2
6
1 2 3 somaXproduto 1 2 3
2 16 54
An interesting point is that you can have a verb of both types. To construct it, you first write the monadic part and then the dyadic part, separated by a :
. See the example:
soma1especial =: verb define
y + 1
:
x + y + 1
)
If the verb is called with only one operand, it will add 1, and if it is called with 2 operands, it will add the two to 1:
soma1especial 1
2
1 soma1especial 1
3
In addition, I have some comments. The result of the verb will be the result of the last sentence (line) of the verb (if I am not mistaken, as in Perl).
Another point worth noting is that, if you create a verb that does not use any operand, you must, invariably, call it passing some value, it may be an empty string:
foo =: verb define
a =: 1
b =: 2
a + b
)
foo ''
3
Note that in this last example I did not define the type of the verb. By default, when you use only verb define
, without adding the dyadic case, the verb will be monadic . Not only does this example show the use of multiple sentences in a single verb.
In addition to this syntax for creating verbs, there is a shorter one for verbs that can be described in just one line:
soma1 := monad : 'y + 1'
soma1especial := dyad : 'x + y + 1'
Notice that the text of the verb must be enclosed in quotation marks.
Finally, I remember that in J, there are also verbs modifiers, such as adverbs, forks, and hooks (and that is a kind of function as well).
The example you gave has two modifiers, the adverb /
and a fork , which has no graphical representation.
The /
will insert the verb to its left between the array elements to its right.
Fork , you need three verbs separated by spaces to work. The result will be a composite function that can be monadic or dyadic . In the first case, that of your example, we would have the following:
(f g h) y
This will be translated as follows:
(f y) g (h y)
In your example, f
is already a modified verb +/
, which will insert +
between y
elements. The result of this will correspond to the operand x
of g
.
Likewise, h
will be replaced by the verb #
, which returns the size of an array and will correspond to the operand y
of g
.
Finally, g
will be replaced by %
, which is a verb dyadic and will divide the results obtained previously.
The difference between a fork
monadic of a dyadic is in its translation. An example would be x (f g h) y
that would be translated to (x f y) g (x h y)
and will work in a way similar to the monadic mode explained above.
Magician, is not it?