Attila Karoly
Attila Karoly

Reputation: 1031

Building an array of verbs in J

Is it possible to build arrays of verbs? I've tried this:

f =: >:
f2 =: f f

There's no syntax error but f2 is clearly not an array of verbs. For instance

f2 yields f f
$ f2 yields $ f2
0 { f2 yields 0 { f2

2 3 $ f behaves in a similar way.

I'd also like to know if verbs can be activated by the name.

Edited

Instead of f2 =: f f, which is a composition, let's have

f =: >:
f2 =: 2 1 $ f

f2 yields 2 1 $ f
$ f2 yields $ f2
0 { f2 yields 0 { f2

It seems that f2 represents the sequence 2 1 $ f, while in the case of an atom, e.g. 2 1 $ 7, the right side is a vector.

Upvotes: 1

Views: 175

Answers (2)

Eelvex
Eelvex

Reputation: 9143

A collection of functions is called a gerund in J and is formed using the tick "`":

g =: +`-`f
┌─┬─┬─┐
│+│-│f│
└─┴─┴─┘

You can use @. to apply the appropriate verb using its index:

4 2 3 (g @. 0) 1 5 6
5 7 9
4 2 3 (g @. 1) 1 5 6
3 _3 _3
4 2 3 (g @. 2) 1 5 6
1 0 0

NB. defining a 2x2 gerund:
k =: 2 2 $ +`-`*`%
┌─┬─┐
│+│-│
├─┼─┤
│*│%│
└─┴─┘

1 (((<0 0) { k) @. 0) 2
3
1 (((<0 1) { k) @. 0) 2
_1
1 (((<1 0) { k) @. 0) 2
2
1 (((<1 1) { k) @. 0) 2
0.5

The index can also be a new verb that depends on the arguments or a list of indices.

Upvotes: 1

bob
bob

Reputation: 4302

As the comments point out, f2=: f f creates a composition and f3=: 2 1 $ f creates a composition as well. In the case of f2 you are defining a hook and in the case of f3 you are defining a fork that will take the results of f when it is given an argument and return the 2 1 shape of that result. The reason that it just parrots back your input is that is the way that J displays verbs without arguments. It shows you the construction, because it does not know what the verb will be applied to.

I think what you may want is the process of making a verb into a gerund, which is the noun form of a verb that can be activated in a number of ways. Creating a gerund is done by inserting a backtick between two verbs or for a single verb, a backtick between the verb and an empty string. Examples of conjunctions that work on gerunds are Agenda and Evoke Gerund.

Although this can be done in J, it is pretty clunky because J does not have the meta-operators that are necessary to manipulate verbs as if they are nouns, aside from turning them into gerunds.

Upvotes: 1

Related Questions