                                   Chapter 11

                        USING J : ADVERBS and CONJONCTIONS


      adverb definition via : in J 3.2
      J questions
      Help with J


*========================================
# adverb definition via : in J 3.2

+------------------
| Michael J. A. Berry
| <MJAB.91Sep19100514@nanna.think.com>

It was recently pointed out to me (by weg@mace.cc.purdue.edu - thanks!)
that the gnue emacs J-interaction mode I posted a while ago does not work
with current versions of J due to spelling changes in the language.

Yesterday I FTP'd version 3.2 (my previous version was 2.9).  I was able to
get function definition working again, but not adverb definition.  I have
gone from :: to : and switched the arguments, but x. does not seem to work
as the formal name of the argument verb.  Can someone tell me what I am
doing wrong?  (Then I will re-post the J-interaction mode).


   b =. i.&.> i.5
   b
++-+---+-----+-------+
||0|0 1|0 1 2|0 1 2 3|
++-+---+-----+-------+
   #&.> b
+-+-+-+-+-+
|0|1|2|3|4|
+-+-+-+-+-+
   each =. 'x.&.>' : 1
   each
+-----+-+-+
|x.&.>|:|1|
+-----+-+-+
   #each b
value error

   eech =. &.>
   #eech b
+-+-+-+-+-+
|0|1|2|3|4|
+-+-+-+-+-+



*========================================
# J questions

+------------------
| Eythan Weg
| <WEG.91Apr2170248@convx1.convx1.ccit.arizona.edu>

Hi there:
I have a few questions to J-wise people:

1) Let us define a conjunction say t=.2 :  ' x. y.'.
   Suppose I do +/ t 1 2.  The answer is 3.  Now suppose a=.2 2 $1 2 3 4.
   I want t to operate on the cells of rank 1 of a.  I do I use the rank
   conjunction to obtain the sum of the rows of a?  I want to use t.

[[NB. sws.  The form of conjunction definition changed; it's now
   t=. ' x. y.' :2
]]

2) Is there a way to use J in a filter like cat file|J|awk|J...?

3) Quite generally can one imagine J acting as a shell?


+------------------
| Raul Rockwell
| <2APR91.18555001@uc780.umd.edu>

Ethan Weg writes:
>1) Let us define a conjunction say t=.2 :  ' x. y.'.
>   Suppose I do +/ t 1 2.  The answer is 3.  Now suppose a=.2 2 $1 2 3 4.
>   I want t to operate on the cells of rank 1 of a.  I do I use the rank
>   conjunction to obtain the sum of the rows of a?  I want to use t.

I presume you meant "How do I use the rank conjunction..."

Note that t is not a verb, and the result of t is not a verb in this
case.  Still, you could try       +/"1 t a

>2) Is there a way to use J in a filter like cat file|J|awk|J...?

Well, you can set yourself up a profile and make it read from standard
input and write to standard output.  I don't think J has developed far
enough yet that I'd want to set things up this way.  (Yet)

>3) Quite generally can one imagine J acting as a shell?

The flexibility and syntax would be nice, eh?

I think, however, that a lot of the elegance of J results because data
objects are fairly static.  The efficiency is way to low to do
character by character stuff, and adding a "character stream" "type"
to J would be essentially a complete re-write.

I suppose step one in this direction would be an efficient
implementation of J.  (Which probably would mean bypassing C and
working in assembler -- which is painful because it's hard to port).

But just because it's impractical doesn't mean it isn't worth
thought....


+------------------
| Roger Hui
| <1991Apr3.123406.20239@yrloc.ipsa.reuter.COM>

1. To find the sum of the rows of matrix a using the conjunction t
that you defined, t=. 2 : 'x. y.', say:  +/"1 t a .

2. J uses only standard-in and standard-out for input and output.


+------------------
| Eythan Weg
| <WEG.91Apr3231451@convx1.convx1.ccit.arizona.edu>

I will revise my first question:
Let t=.2 : '(x.1{.y.),_1{.y.'.  Now  ^&2 t 2 3 should produce 4 3.
I want to see t as a black box which operates on the right on vectors.
Now how do I deal with a matrix whose rows are supposed to serve as
a series of right operands? How can I force the conjunction to behave
as if it is a verb with a specified right rank?  So if a=.i.4 then
^&3 t "1 a will give 2 2$0 1 8 3.  My problem is that " works
essentially on verbs.
Thank you.  Eythan

[[NB. sws. the above needs to be
   t=. '(x.1{.y.),_1{.y.' :2
]]

+------------------
| Raul Rockwell
| <4APR91.19411393@uc780.umd.edu>

Eythan Weg writes:
>I will revise my first question:
>Let t=.2 : '(x.1{.y.),_1{.y.'.  Now  ^&2 t 2 3 should produce 4 3.
>I want to see t as a black box which operates on the right on vectors.

t =. 2 : '(x.1{."1 y.), _1{."1 y.'

or

t=. 1 : '(x.@{.),_1&{.'

then    ^&2 t   is a verb

[[NB. sws. This is now

   t=. '(x.1{."1 y.), _1{."1 y.' :2

or
   t=. '(x.f.@{.),_1&{.' :1

]]


+------------------
| Raul Rockwell
| <4APR91.20073073@uc780.umd.edu>

[[[NB. sws. ... here ]]

I wrote:

>t=. 1 : '(x.@{.),_1&{.'


I should have said

t=. 1 : '(x.@(1&{.)) , _1&{.'

oops...


[[NB. sws.  this is now

t=. '(x.@(1&{.)) , _1&{.' :1

]]


+------------------
| Roger Hui
| <1991Apr5.015530.4750@yrloc.ipsa.reuter.COM>

In article <WEG.91Apr3231451@convx1.convx1.ccit.arizona.edu> weg@convx1.ccit.
arizona.edu (Eythan Weg) writes:

>I will revise my first question:
>Let t=.2 : '(x.1{.y.),_1{.y.'.  Now  ^&2 t 2 3 should produce 4 3.
>I want to see t as a black box which operates on the right on vectors.
>Now how do I deal with a matrix whose rows are supposed to serve as
>a series of right operands? How can I force the conjunction to behave
>as if it is a verb with a specified right rank?  So if a=.i.4 then
>^&3 t "1 a will give 2 2$0 1 8 3.  My problem is that " works
>essentially on verbs.

The rank conjunction " does indeed work only on verb right arguments.
If f is verb, then
  f"0 x    applies f to each rank-0 cell (each scalar) of x
  f"1 x    applies f to each rank-1 cell (each vector) of x
  f"k x    applies f to each rank-k cell of x
(Try  x=.i.2 3 4 5  and  f=.<  or  f=.+/  )

No other conjunction needed.  If you insist on having this conjunction t,
it is for reasons that you have not yet mentioned.



*========================================
# Help with J

+------------------
| Ian Wild
| <1991Dec13.135136.1535@siesoft.co.uk>

I'm sure this must be quite simple, but I can't figure it out at all.  I'm
trying to define a conjunction (CONJ) that takes two verbs, and returns a
verb that's equivalent to just writing the two verbs next to each other.

i.e.

  z =. u CONJ v
  z n           is equivalent to        u v n
  m z n         is equivalent to        m u v n

I've tried various hooks, forks, @s, &s and scatterings of [.]., but I can
never get both the unary and binary cases to come out OK.  Does anyone want
to let me in on the secret?


+------------------
| Roger Hui
| <1991Dec29.223929.12598@yrloc.ipsa.reuter.COM>

Ian Wild writes:
>  z =. u CONJ v
>  z n           is equivalent to        u v n
>  m z n         is equivalent to        m u v n

The conjunction can be defined   CONJ =. @ : (`\)   .  It may be
instructive to follow the steps in the application of CONJ:

u CONJ v
u (@ : (`\)) v                 substitution
(u @ v) : (u (`\) v)           c1 c2 c3  (Dictionary page 5)
(u @ v) : ((u`v)\)             c1 a2     (page 5)
(u @ v) : (u v)                gerund \  (page 13)

u CONJ v y
(u @ v) : (u v) y              substitution
u @ v y                        :         (page 10)
u v y                          @         (page 15)

x u CONJ v y
x (u @ v) : (u v) y            substitution
x (u v) y                      :         (page 10)
x u v y                        hook      (page 5)



 