How to create a function in the J language?

Asked

Viewed 401 times

7

How to create a function in the J language? All the examples I found online show only how to describe functions through a Fork:

media =: +/ % #
media 1 2 3 4
2.5

The code above (+/ % #) is the same as "add all array elements" (+/), "find the array size" (#) and divide each other (%). Which is okay, but I’d like to know more generally, how to define functions with, say, multiple parameters, multiple expressions in the body of the function, etc. does J support this sort of thing? Or this language has no functions such as the most common languages?

  • 1

    The question that won’t shut up, who in the world shows in J?

  • @Pablopalaces Good, the staff of codegolf.SE seems to use a lot, why will it be right? rsrs

  • now it makes perfect sense!

1 answer

3


I’m no expert on J, but I looked for some information that might help you.

Well, if you took a look at the documentation, you saw that word Function is not so common in J. Henry Rich, writer of J for C programmers, assimilates function to the term verb in J.

As this same author attests, verbs can only take up to two arguments (or, in the idiomatic of J, operands). Thus, we have two types of verbs, monadics or dyadics, which receive one and two operands respectively.

Because of this, J names the operands automatically for you. In the case of a verb monadic, the name that will be passed to the sentences of the verb body will be the y. At the time of calling a verb monadic, the operand should be right of the verb.

To build a verb monadic:

soma1 =: monad define
y + 1
)

Note that you can pass both a single number and an array to the verb it will work:

soma1 1
2
soma 1 2 3
2 3 4

In verbs Dyadic, the names of the operands will be x and y. At the time of calling the verb, x should be on your left and y should be on your right. To build a verb Dyadic:

somaXproduto =: dyad define
(x + y) * x * y
)

The same observation about the types of operands made for verbs monadics valley for the 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 the two types. To build it, you write the part first monadic and then the part Dyadic, separated by a :. Take 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 called with 2 operands, it will add the two to 1:

soma1especial 1
2
1 soma1especial 1
3

Also, I have some remarks. The verb result will be the result of the last sentence (line) of the verb (if I’m not mistaken, as in Perl).

Another noteworthy point is that if you create a verb that does not use any operand, you must inevitably call it by 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 verb type. By default, when you use only verb define, without adding the case Dyadic, the verb will be monadic. Not only, this example shows the use of multiple sentences in a single verb.

In addition to this syntax to create verbs, there is a shorter one, for verbs that can be described in only one line:

soma1 := monad : 'y + 1'
soma1especial := dyad : 'x + y + 1'

Note that the text of the verb should be in quotation marks.

Finally, I remember that in J, there are also verb modifiers, such as adverbs, Forks and Hooks (and that are still a function type as well).

The example you gave presents two modifiers, the adverb / and a Fork, which has no graphical representation.

The / will insert the verb that is on your left between the elements of the array that is on your right.

Already the Fork, needs three space-separated verbs to work. The result will be a composite function that can be monadic or Dyadic. In the first case, your example, we would have the following:

(f g h) y

This will be translated as follows:

(f y) g (h y)

In his example, f is already a modified verb +/, who will insert + among the elements of y. 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 corresponds to the operand y of g.

Finally, g will be replaced by %, which is a verb Dyadic and divide the results obtained previously.

The difference between a fork monadic of a Dyadic is in your translation. An example would be x (f g h) y which would be translated to (x f y) g (x h y) and shall operate in an annual manner in accordance with monadic explained earlier.

Magician, no?

  • Excellent! For me it is already accepted (but as usual, I will wait a little longer given the reward). It’s funny that, without knowing this nomenclature ("verb"), I had enough trouble finding this information. Now it’s all much clearer!

  • @mgibsonbr The book I put as reference is a good reading, and fulfills very well these gaps between human languages and J.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.