Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why is this so difficult in J?

8 views
Skip to first unread message

pineapple

unread,
Sep 12, 2009, 12:48:32 PM9/12/09
to
APL:
x←2
y←1 2 3
x⌷y
2
⌷/x y
2

J:
x=.2
y=.0 1 2
x{y
2
{/ x y
|syntax error
| {/x y
{/ x;y
|length error
| {/x;y
{/"1 x;y
|length error
| {/"1 x;y
{/"2 x;y
|length error
| {/"2 x;y
{"0/ x;y
|length error
| {"0/x;y
{"1/ x;y
|length error
| {"1/x;y
{"2/ x;y
|length error
| {"2/x;y

NB. gives up...

TikkaNZ

unread,
Sep 12, 2009, 9:05:16 PM9/12/09
to
The quick answer:

{&>/x;y
2

The long answer:

J doesn't use strand notation so:
x y
|syntax error
| x y

You need to link them as you suspected:
x;y
+-+-----+
|2|0 1 2|
+-+-----+

However now x and y are boxed and:
(<x) { (<y)
|length error
| (<x) {(<y)

There is only 1 item in <y hence the length error.
What you want to do is insert a verb that will unbox the left and
right arguments before using {
hence:
(<x) {&> (<y)
2
And:
{&>/ x;y
2

Just because it is different to the APLs you are used to doesn't mean
it is hard. I imagine (never having used it myself) that Sharp APL
would be similar to J in this respect - the difference is the box
model. The J for APL article that you were pointed to a few days ago
will probably be enlightening there.

pineapple

unread,
Sep 13, 2009, 12:23:01 PM9/13/09
to
Very helpful, thanks.

> The J for APL article that you were pointed to a few days ago
> will probably be enlightening there.

I've read all of them, but they didn't help here, because I couldn't
find an example of what I wanted to do.

By the way, do you know why J doesn't support strand notation?
Wouldn't it simplify many things?

TikkaNZ

unread,
Sep 13, 2009, 5:55:39 PM9/13/09
to
On Sep 14, 4:23 am, pineapple <pineapple.l...@yahoo.com> wrote:
> Very helpful, thanks.
>
> > The J for APL article that you were pointed to a few days ago
> > will probably be enlightening there.
>
> I've read all of them, but they didn't help here, because I couldn't
> find an example of what I wanted to do.

Sorry, I was talking about the boxed/nested array model in general
rather than your particular problem. However understanding one will
help with the other.

> By the way, do you know why J doesn't support strand notation?
> Wouldn't it simplify many things?

From my reading the answer to that is: some things would be simpler,
other things (for example the parsing model) would be more complicated
or ambiguous. If you want to follow up on this, searching the J forum
for strand notation brings up a number of interesting threads. Here
are a couple of posts that address the question:
http://www.jsoftware.com/pipermail/general/2005-August/023853.html
http://www.jsoftware.com/pipermail/general/2004-May/017539.html

Phil Last

unread,
Sep 14, 2009, 12:26:54 AM9/14/09
to

Hello anonymous (unless your name really is TikkaNZ)

Thanks for these two links the first of which was totally coherent and
left me feeling that I still don't know which is the "better" (most
consistent, comprehensible) implementation of nested arrays.

The second seemed to contain a fair amount of sophistry and I need to
read it again with a reduced ethanol count.

Good luck whoever you are.

Bob Smith

unread,
Sep 14, 2009, 1:26:44 PM9/14/09
to
On 9/13/2009 5:55 PM, TikkaNZ wrote:
> On Sep 14, 4:23 am, pineapple <pineapple.l...@yahoo.com> wrote:
>> Very helpful, thanks.
>>
>>> The J for APL article that you were pointed to a few days ago
>>> will probably be enlightening there.
>> I've read all of them, but they didn't help here, because I couldn't
>> find an example of what I wanted to do.
>
> Sorry, I was talking about the boxed/nested array model in general
> rather than your particular problem. However understanding one will
> help with the other.

There is a succinct description of why APL2 (and others) chose the model
for nested arrays in which enclose of a simple scalar is a simple
scalar: "The Origins of APL2" by Jim Brown, APL94 Conference
Proceedings. There he presents an identity for outer product
generalized to arbitrary functions and nested arrays (and limited to
vector arguments for exposition purposes only):

(L ∘.f R)[I;J] ←→ ⊂(⊃L[I]) f ⊃R[J]

Now let's check it for compatibility on simple arrays and primitive
scalar dyadic functions such as

2 3 ∘.× 4 5 6

According to the right side of the above identity, the first item of the
result is

⊂ (⊃2) × ⊃4

At this point, we see why the disclose of a simple scalar is defined to
be the same simple scalar -- otherwise it would have to be an error
which isn't very helpful. Substituting, this yields

⊂ 2 × 4

At this point, we see why the enclose of a simple scalar is defined to
be the same simple scalar -- to make the result compatible with our
previous understanding of outer product.

Note that although the above description uses numeric vector strands,
they could just as easily be replaced by (2,3) and (4,5,6) to make it
independent of strands and without changing the conclusions.

--
_________________________________________
Bob Smith -- bsm...@sudleydeplacespam.com

To reply to me directly, delete "despam".

TikkaNZ

unread,
Sep 14, 2009, 6:13:03 PM9/14/09
to
On Sep 14, 4:26 pm, Phil Last <phil.l...@ntlworld.com> wrote:
> On Sep 13, 5:55 pm, TikkaNZ <r.g.sherl...@massey.ac.nz> wrote:
>
> > On Sep 14, 4:23 am, pineapple <pineapple.l...@yahoo.com> wrote:
>
> > > By the way, do you know why J doesn't support strand notation?
> > > Wouldn't it simplify many things?
>
> > From my reading the answer to that is: some things would be simpler,
> > other things (for example the parsing model) would be more complicated
> > or ambiguous. If you want to follow up on this, searching the J forum
> > for strand notation brings up a number of interesting threads. Here
> > are a couple of posts that address the question:http://www.jsoftware.com/pipermail/general/2005-August/023853.htmlhtt...

>
> Thanks for these two links the first of which was totally coherent and
> left me feeling that I still don't know which is the "better" (most
> consistent, comprehensible) implementation of nested arrays.

For me that is the point! Robust discussion of the last ~25 years
hasn't provided a consensus that one is "better" than the other.
Perhaps they both have their pros and cons? I started out using APL
+Win's implementation of the APL2 model and now use the Sharp APL / J
model in J. I currently prefer the later, but I'm sure that is at
least slightly influenced by my current programming language of
choice! ;-).

The original question was "why doesn't J support strand notation?".
The links I provided and the threads around them bring up some reasons
that it is not as simple as it may seem at first glance. Here are some
posts more targeted on strand notation itself in J.

Firstly a proposal for implementing it. If you follow the thread, the
next 3 or so messages give reasons that it would not be a good idea.
http://www.jsoftware.com/pipermail/general/2005-August/023835.html

Is strand notation/juxtaposed nouns a possibility in some future
version of J?
http://www.jsoftware.com/pipermail/general/2005-August/023905.html

pineapple

unread,
Sep 14, 2009, 7:55:12 PM9/14/09
to
I have always found J's "amend" difficult and non-intuitive to work
with. Whenever I use it, I never naturally remember how it works. I
always have to sit down for 10 minutes and jerk around with it until I
get a construct that works. I just realized what part of the
difficulty is. It looks like it uses "strand" notation, even though J
doesn't have it:

y =. 1 2 3 4 5 6
z =. 5
zz =. 0
(zz,z)}y
|rank error
| (zz,z)}y
zz z} y
1 2 3 4 5 0

Oddly enough, the "strand" doesn't work with non-variables:

y =: 1 2 3 4 5 6
0 5} y
|rank error
| 0 5}y
(0,5)}y
|rank error
| (0,5)}y
(0;5)}y
|rank error
| (0;5)}y
(0) 5}y
1 2 3 4 5 0

Anyone care to explain what is going on with this? How does "amend"
reference two "strand" variables on its left side?

TikkaNZ

unread,
Sep 14, 2009, 10:06:07 PM9/14/09
to

I wonder if these sorts of questions might better be directed to the J
programming forum?
http://www.jsoftware.com/jwiki/System/Forums

Anyway I'll give it a shot:
The key here is that } (amend) is not a verb, it is an adverb. Adverbs
"combine" with the verb or noun immediately to their left to form a
new verb - just like / (insert) combines with + (plus) to form the new
verb +/ (sum). The only difference is that insert is combining with a
verb (+), whereas in the form: x m} y , amend is combining with a
noun (m) to create a new verb (m}). You could describe (m}) in words
like this: "replace the items m{y with the items of x".

y =: 1 2 3 4 5 6
0 5} y
|rank error
| 0 5}y

} combines with the noun to its left (0 5) to form a new verb (0 5}) ,
but there is no x argument.
Note that this form would be valid with the correctly shaped y
argument (hence the rank error), but it is not what you intend.

(0,5)}y
|rank error
| (0,5)}y

(0,5) is the same as 0 5 in the last example, so same problem.

(0;5)}y
|rank error
| (0;5)}y

Still no x argument because (0;5) is a noun.

(0) 5}y
1 2 3 4 5 0

Here amend takes the noun to its left (the 5) and makes a new verb
(5}) that could be described in words as "replace the item 5{y with
x".

It might make it clearer to surround amend and the noun to its left
with brackets:
0 (5}) y


1 2 3 4 5 0

HTH

pineapple

unread,
Sep 14, 2009, 11:09:09 PM9/14/09
to
On Sep 15, 9:06 am, TikkaNZ <r.g.sherl...@massey.ac.nz> wrote:

> I wonder if these sorts of questions might better be directed to the J
> programming forum?http://www.jsoftware.com/jwiki/System/Forums

I apologize. I was under the "loose impression" (probably closer to
an assumption) that it was okay to post J questions here (it being a
"dialect" of APL, it being discussed with APL at places like
vector.org, etc). I will try to refrain in the future.

Thanks for the help. Enlightening.

TikkaNZ

unread,
Sep 15, 2009, 12:59:06 AM9/15/09
to

Sorry, I didn't mean to come across as the Newsgroup Police ;-)
I'm not sure what the "policy" is, or even if there is one, but I do
remember some users getting touchy about it in the past. I think J
used to be discussed here, but the volume of traffic was such that "it
was decided" that it should have its own forum. My opinion FWIW would
be that posts relevant to both J and APL would be fine, but that if
you want to ask "how do I do this in J" questions, you'd be "better
off" posting to the J programming forum. "Better off" meaning: more
likely to get good answers, and less likely to aggravate anyone!

>
> Thanks for the help. Enlightening.

No problem!

Kerry Liles

unread,
Sep 15, 2009, 9:22:31 AM9/15/09
to
"pineapple" <pineapp...@yahoo.com> wrote in message
news:20774c05-b22a-49d6...@i4g2000prm.googlegroups.com...

I would encourage you to reconsider. I am not sure about everyone else, but
I believe your posts are absolutely on-topic for this newsgroup.


pineapple

unread,
Sep 16, 2009, 2:00:19 PM9/16/09
to
On Sep 15, 8:22 pm, "Kerry Liles"
<kerry.removethisandoneperiod.li...@gmail.com> wrote:

> I would encourage you to reconsider. I am not sure about everyone else, but
> I believe your posts are absolutely on-topic for this newsgroup.

Thanks. I will say in my defense that I did post APL code (at the
top). My question was why something seemed so easy in APL, and so
difficult in J. In light of that, my post seems on topic.

Björn

unread,
Sep 19, 2009, 2:11:11 AM9/19/09
to

One thing to note about the difference between APL2 and SAPL (and thus
J) is that when you wrap something into a package and then wrap it
again and again with one layer after the other and then you unwrap it
again one layer at a time.

In SAPL you add the layers one at a time and you unwrap it the same
number times.

In APL2 it is not always so and that is the major difference between
the two (or three).

Once you have a scalar in APL2 you can not wrap it so a wrapped scalar
is the same as the scalar.

Those who favor SAPL way of doing this find it concise and correct.

Those who favor APL2 find it easy because many simple things become
easy.

The same is true of the way you do the wrappings.

In SAPL you always need to specifically do the wrapping.

In APL2 you do not always need to do the wrapping it just happens.

Same again.

Those who favor SAPL way of doing this find it concise and correct.

Those who favor APL2 find it easy because many simple things become
easy.


0 new messages