Harvey,
I completely concur with Oleg's comments on the topic -
and Devon's improvement on my translation of your line.
At 02:34 -0700 2006/08/07, Oleg Kobchenko wrote:
In general, rather than trying to find literal
translation, such as axis, it is better to relate
the meaning, such as append vectors into a matrix.
And it's very helpful to have an example with actual
data for illustration.
--- Devon McCormick <[EMAIL PROTECTED]> wrote:
or even
attk-.0
or, more fully in the context of the original question
z=. r,:0-.~+/z
>
But to answer your comments/questions -
The only APL I know of that had default index origin
of 0 was SAX, which in many ways was a predecessor of
J. Please note that J has ONLY origin 0.
There are some good sources of examples in J - one
is Henry Rich's "J for C Programmers" and since you
say your background isn't strong in APL, this would
be an excellent thing for you to browse. Henry does
offer lots of real and useful examples.
At 23:44 -0600 2006/08/08, PackRat wrote:
I'm looking at J like learning a non-English language. When you're a
beginner, you tend to want lots of patterns and examples to follow.
When you translate from one language to another, you initially tend to
do a lot of word-by-word translation. The semantic facility with
language that Oleg mentioned in his response only comes after a lengthy
acquaintance with a language, when you develop a "feel" for the
language and you become able to understand phrases and idioms and
nuances that may not make sense to a beginner. It's at the more
advanced stage that you can create literary works rather than language
exercises. (Elegant J programs are the programming equivalent of
"literary works".)
You have a good approach, but you must realize that
the only real advantage of j (and/or APL) is that
they offer a different semantic view of problems.
Oleg's point is that you will progress much more
quickly if you describe and understand the problem
rather than trying to translate something that may
not be the best way to view/solve the problem...
This is important, even from the beginning -- for
example, things like +/ are important concepts that
don't generally exist in other languages, especially
the idea of generalizing to */ -/ or even foo/ --
these things require thinking in terms of concrete
structures (arrays) and that is a big step to getting
past the beginning points in either APL or J. Things
around the APL "axis notation" as opposed to J's
concept of function rank is very important to study
and play with until it is understood, not just copied
in examples.
Previous to my asking my question, I had come up with this:
z =. r , [_0.5?] ( 0 ~. attk ) / attk =. +/[0?] z
where the square-bracketed stuff was my questions.
This compares favorably with yours:
z =: r ,: (0 ~: attk)#attk =: +/z
Now I have an additional question: was your "#" a typo? I presumed the
"/" would stay the same between languages.
No - # was not a typo and / is not used as "compression"
in J as it was in APL. Even the use of / with a verb on the
left is a bit different in J than APL, e.g. / inserts
its left verb between items, hence seems to operate along
a different axis than APL / ... Again, examples are best:
i.2 3
0 1 2
3 4 5
+/i. 2 3
3 5 7
NB. in APL the result would have been 3 12
NB. +/ creates a new verb so in place of APL outer product
1 2 3 */ 4 5 6 7
4 5 6 7
8 10 12 14
12 15 18 21
NB. These are fundamental differences that you need to
NB. assimilate before trying to find translation patterns...
1 2 0 0 3 2 1 # 'abcdefg'
abbeeeffg
But in the numeric case that you specified. The use of -.
(as suggested by Devon) is better - e.g.
1 2 0 0 3 2 1 -. 0
1 2 3 2 1
That is, remove the 0's Devon wrote it as 0 -.~ ...
for the flow of the expression. ~ reverses the arguments of
the verb to its left - e.g.
2 3 4 %~ 2
1 0.666667 0.5
Once again, the important thing is to practice these
generalizations and learn to think in terms of using
them to advantage. Verb rank is extremely important
in that regard - ~ mainly avoids clumsy expressions
with lots of ()'s
And one more question: since my variable assignments are all local
variables within a function, I presumed that "=." and "~." were the
proper choices. Is that a correct understanding? If not, could you
explain why "=:" and "~:" would be better choices for local variable
assignments within a function?
Thanks in advance for clarifying all of these things for me!
In the examples you gave, it wasn't clear that the
expressions involved local variables. It is the case that
variables in a J script are local unless explicitly global -
the opposite of APL - so your comment is correct that if in
a verb/function, the preference is to use local assignment ( =. )
but ~: isn't assignment at all, but (dyadically) the verb for
not-equal.
You are off to a good start - keep asking questions, but try
examples as much as you can and try to give concrete examples
including sample data.
- joey
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm