Subject: +/i., Gaus, and J
From: mslamm@pluto.cc.huji.ac.il (Zvi Lamm)
Date: 17 Jan 1995 19:31:00 GMT

There is a famous anecdote about Gaus finding the sum of the integers 1
to 100, by using the formula n(n+1)/2. The exaplanation is that he noted
that if you sum up opposite numbers from this vector you always get 101.
So he just did 101*100/2.
Graphically:
1 + 2 + 3 + 4 +
4 + 3 + 2 + 1 =
5 + 5 + 5 + 5 = (4+1)*4
So
1 + 2 + 3 + 4 = (4+1)*4 / 2

Just as an exercise for myself I tried to capture this process in J. I
made a small mistake along the way, which made me think about how
practical can J be in programming. I'd like to here what other think.

My first attempt was this:
(all you have to do is sum >:i. and its reverse and half the result..)
gaus=.(0.5&*)@(+/@((>:@i.)+((-i.)+))))
                           ^^^^^^^^^^^

Now this has a mistake. The verb I marked was meant to create the reverse
of >:i. . My idea was to add n to _0 _1 _2 ... _(n-1)
Since in a hook the combining verb is the left one, it turned out that
what I wrote had the effect of subtracting from n the vector 0 1 2..n.
This produced exactly the same result!!

So I tried to write +(-@i.) which means addtition and since I made a
mistake I made J use subtraction. From the results I couldn't tell I made
a mistake... As a language J didn't help me because the language has an
interpretation for both expressions.

The power of J comes from being so terse, but it also makes the language
much harder to use. This maybe one of the reasons APL like languages fail
- it is easy to make a mistake which will be hard to detect using syntax
alone.

P.S
1. Now I know I can use -i. instead of both versions. This is much better..
2. I learned some J hooks and crooks from this...

Ehud Lamm

