On Sun, Apr 19, 2015 at 4:13 AM, Jon Hough <[email protected]> wrote:
> I am still unsure about a few things, hopefully someone can clear these
up.
> 1. If I have my nxn gerund array and I want to evaluate it on a list of n
items,
> such that the first row takes the first item, the second row takes the
second
> item etc.
> e.g. (*:`%: ,. +:`^) VRB 1 2
I guess I am mildly dismayed that the following was not obvious:
VRB=: 4 :'x`:6 y'"0
(*:`%: ,. +:`^) VRB 1 2
1 2
1.41421 7.38906
The behavior you describe ("I want to evaluate it on a list of n items,
such that the first row takes the first item, the second row takes the
second item etc.") is exactly the behavior of a rank zero verb (given those
argument ranks).
I do understand that learning, education, and humans in general need
repetition of ideas to get them to stick, but I also am getting the idea
that I am not very good at conveying concepts. And I do not know what to do
about that.
I guess I should keep trying.
> 2. This problem is a little bit more abstract.
> If I have a list of gerunds (1D this time), and I want to modify each
verb in the gerund in some way, and then return the sum of the verbs, I am
not sure how to do this.
> For example, if G =: *:`+:`^ and list =. 3 4 5
> I want an adverb or conjunction that returns a verb of the form (3&*@:*:)
+ (4&*@:+:) + (5&*@:^)
For this, first you need to figure out how to deal with one item on each
side. That's relatively simple - since there is no repetition involved you
can use a conjunction:
*:2 :'n&*u'3
3&* *:
Once you have that, you need to figure out how to combine them. The natural
way of combining them would involve a verb (since repetition is involved).
But, in the absence of taking advantage of a quirk which can be found using
a forum search on the phrase: absolute power corrupt (except put an 's' on
the end of the word 'corrupt' - I am leaving that off here so this message
will not show up for that search), the result of a verb is a noun.
So... we need to understand what the gerund form of your result looks like
(and then we can plan on using `:6 on that noun, to turn it into a verb).
So that means inspecting
((3&*@:*:) + (4&*@:+:) + (5&*@:^))`''
But that result is a bit too large for an email message (long lines get
mangled), so let's look at a simpler but similar verb here, and focus on
its high level structure:
(1:+2:+3:)`''
+------------------------+
|+-+--------------------+|
||3|+--+-+-------------+||
|| ||1:|+|+-+---------+|||
|| || | ||3|+--+-+--+||||
|| || | || ||2:|+|3:|||||
|| || | || |+--+-+--+||||
|| || | |+-+---------+|||
|| |+--+-+-------------+||
|+-+--------------------+|
+------------------------+
Or if that gets mangled by your email reader's font handling (you should be
reading this with a fixed width font), go evaluate either of the above
expressions for yourself.
But you should be able to see a pattern there - let's take an even simpler
case:
(1:+2:)`''
+-------------+
|+-+---------+|
||3|+--+-+--+||
|| ||1:|+|2:|||
|| |+--+-+--+||
|+-+---------+|
+-------------+
Now we just need to figure out the types and the shapes of each of these
things and we can build the result "manually":
Here's the shapes:
$L:0 (1:+2:)`''
+-----------+
|+-+-------+|
||1|+-+-+-+||
|| ||2|1|2|||
|| |+-+-+-+||
|+-+-------+|
+-----------+
$L:1 (1:+2:)`''
+-----+
|+-+-+|
||1|3||
|+-+-+|
+-----+
$L:2 (1:+2:)`''
+-+
|2|
+-+
$ (1:+2:)`''
1
As for the types, except at the level 0 "leaves", all of the types here are
boxed. So what are the leaves ?
(1 {. 0 # ])L:0 (1:+2:)`''
+-----------+
|+-+-------+|
|| |+-+-+-+||
|| || | | |||
|| |+-+-+-+||
|+-+-------+|
+-----------+
All leaves in this example have the space character for fill, so all leaves
are characters. Though of course two of those leaves will be externally
supplied gerunds. But do we really need the one element lists to be lists?
It would be so much easier if we could just use rank 0 nouns there. A
little experiment suggests that that simplification would be fine:
(<'3';<'1:';'+';'2:')`:6
1: + 2:
(<'3';<'1:';'+';'2:')`:6''
3
So, we want a verb like that, but replacing '1:' with x and replacing '2:'
with y
(Is anyone even reading at this point? Or is this kind of description too
boring? I suppose that you have to perform some experiments or the ideas
will not actually make sense - even though they might look like they make
sense...)
So here's the verb:
4 :'<''3'';<x;''+'';y'
No, that's not actually right. Because x and y are going to already be
boxed - so we don't want to box them again. But that's a simple fix:
4 :'<''3'';<x,''+'';y'
But those doubled quotes are annoying, so let's make this tacit instead:
'3' <@; [ <@, '+' ; ]
And here, I'd like to pause for a moment and call out a similarity between
this tacit verb and explicit verb definitions. If you squint, you can sort
of see the similarity. The notation is, of course, different, but the
similarity is an intentional part of the design of the language.
So we are going to want to use something like ('3' <@; [ <@, '+' ; ])/ ...
where ... is the result of our earlier conjunction (2 :'n&*u') on each of
elements from x and y of the verb which ... ahh ... that's getting too hard
to describe. Here's what we are going to reduce:
*:`+:`^ 4 :'{.((x`:6) 2 :''n&*u''y)`'''''"0 (3 4 5)
That just kind of rolls off the tongue, doesn't it?
Er... actually, it's probably an unreadable mess, since you didn't write it
yourself. So let's break it apart and give things names:
term=: '3' <@; [ <@, '+' ; ]
Factor=: 2 :'n&*u'
Ing=: `''
Er=: `:6
factoring=:4 :'{. (x Er Factor y)Ing'
G =: *:`+:`^
list =. 3 4 5
You should recognize the last two names, from earlier...
The name 'Ing' is because in english we form gerunds by using the suffix
-ing on a verb. Similarly, the name 'Er' is because I needed a word that
conveyed action, and the suffix -er is used in english to indicate one who
does something. Not ideal, but maybe a little evocative?
Likewise, 'Factor' is because I needed a name and I decided that the
multiplication involved was the important thing. (By the way, you'll
probably not actually be wanting that expression - you'll probably really
be wanting to eliminate the & from that definition.) Same thing for 'term'
- I'm sort of borrowing from the mathematical vocabulary sometimes used to
describe polynomial-like algebraic equations.
Anyways, with this out of the way, the expression you said you wanted looks
like this:
(term/G factoring"0 list)`:6
(3&* *:) + (4&* +:) + (5&* ^)
Simple, no?
Well, when you consider that writing this kind of program in another
language would take considerable thought and/or a lot of code, it actually
is rather simple. But it's not entirely trivial, either. Also, note that
the parenthesized expressions are hooks, and that's probably not going to
do what you want. What you really want is probably more like:
Factor=: 2 :'n*u'
(term/G factoring"0 list)`:6
(3 * *:) + (4 * +:) + 5 * ^
So that might actually be a little scary - it suggests that you have not
been performing experiments on your sentences to ensure that they do what
you want. This is a problem I also often have - I think I know what I am
talking about but until I try it out and see how bad it gets, I don't
really understand.
Still to get here I also imagine you would need to have spent some time
working with J's grammar rules (the Parsing and Execution appendix in the
dictionary and the j's trace facility are both good resources - but you
also need to pace yourself, so you can come back to it on multiple
different days). A formal computer science class on parsing might also be
useful background.
Finally, I'll point out that I included a {. in the above lump of code,
without any explanation. Do you see why I did that? (There's no error if it
is omitted, so it's really a matter of personal taste. But I imagine -
perhaps incorrectly - that if you are performing experiments to see what
intermediate results are like that you'll see why I included it.
Anyways, I hope this helps.
And, even if this turns out to not be helpful, it was an entertaining
exercise for me.
Thanks,
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm