Reputation: 722
I am trying to understand composition in J, after struggling to mix and match different phases. I would like help switching between monadic and dyadic phrases in the same sentence.
I just made a simple dice roller in J, which will serve as an example:
d=.1+[:?[#]
4 d 6
2 3 1 1
8 d 12
10 2 11 11 5 11 1 10
This is a chain: "d is one plus the (capped) roll of x occurrences of y"
But what if I wanted to use >: to increment (and skip the cap [: ), such that it "switched" to monadic interpretation after the first fork? It would read: "d is the incremented roll of x occurrences of y".
Something like this doesn't work, even though it looks to me to have about the right structure:
d=.>:&?[#]
d
>:&? ([ # ])
(If this approach is against the grain for J and I should stick to capped forks, that is also useful information.)
Upvotes: 3
Views: 95
Reputation: 9143
To summarize the main simple patterns of verb mixing in J:
(f @: g) y = f (g y) NB. (1) monadic "at"
x (f @: g) y = f (x g y) NB. (2) dyadic "at"
x (f &: g) y = (g x) f (g y) NB. (3) "appose"
(f g h) y = (f y) g (h y) NB. (4) monadic fork
x (f g h) y = (x f y) g (x h y) NB. (5) dyadic fork
(f g) y = y f (g y) NB. (6) monadic hook
x (f g) y = x f (g y) NB. (7) dyadic hook
A nice review of those is here (compositions) and here (trains).
Usually there are many possible forms for a verb. To complicate matters more, you can mix many primitives in different ways to achieve to same result.
Experience, style, performance and other such factors influence the way you'll combine the above to form your verb.
In this particular case, I would use @bob's d1
because I find it clearer to read: increase the roll of x copies of y
:
>: @ ? @ $
For the same reason, I am replacing #
with $
. When I see #
in this context, I automatically read "number of elements of", but maybe that's just me.
Upvotes: 5
Reputation: 4302
Let's look at a dyadic fork a(c d f h g)b
where c,d,f, g and h are verbs and a and b are arguments, which is evaluated as: (a c b) d (a f b) h (a g b)
The arguments are applied dyadically to the verbs in the odd positions (or tines c,f and g) - and those results are fed dyadically right to left into the even tines d and h. Also a fork can be either in the form of (v v v) or (n v v) where v stands for verbs and n stands for nouns. In the case of (n v v) you just get the value of n as the left argument to the middle tine.
If you look at your original definition of d=.1+[:?[#]
you might notice it simplifies to a dyadic fork with five tines (1 + [: ? #)
where the [ # ]
can be replaced by #
as it is a dyadic fork (see definition above).
The [:
(Cap) verb returns no value to the left argument of ?
which means that ?
acts monadically on the result of a # b
and this becomes the right argument to +
which has a left argument of 1
.
So, on to the question of how to get rid of the [:
and use >:
instead of 1 + ...
You can also write ([: f g)
as f@:g
to get rid of the Cap, which means that ([: ? #)
becomes ?@:#
and now since you want to feed this result into >:
you can do that by either:
d1=.>:@:?@:#
d2=. [: >: ?@:#
4 d1 6
6 6 1 5
4 d2 6
2 3 4 5
8 d1 12
7 6 6 4 6 9 8 7
8 d2 12
2 10 10 9 8 12 4 3
Hope this helps, it is a good fundamental question about how forks are evaluated. It would be your preference of whether you use the ([: f g)
or f@:g
forms of composition.
Upvotes: 4