23
2
Presented in honor of APL as an interactive tool turning 50 this year
Background
Ken [Iverson] presented his paper Formalism in Programming Languages in August 1963 at a Working Conference on Mechanical Language Structures, Princeton, N.J. The list of conferees is full of famous and soon-to-be famous names, and a few future Turing Award winners (Backus, Curry, Dijkstra, Floyd, Iverson, Newell, Perlis, Wilkes). The paper also records the discussion that occurred after the presentation, ending with an exchange between Ken and [Edsger] Dijkstra, in which Ken’s reply to Dijkstra’s question was a one-liner.
Challenge
How would you represent a more complex operation, for example, the sum of all elements of a matrix M which are equal to the sum of the corresponding row and column indices?
Write a snippet or expression (no need for a full program or function) to calculate the sum of each element in a given integer matrix which is equal to the sum of its indices. Or, as FryAmTheEggman puts it: given a matrix M with elements aij return the sum of each aij where aij = i + j.
You may assume the matrix already being in a variable or memory location, or you may take it as an argument or input. You may use either 0 or 1 based indices.
Test cases
0
for empty matrix
2
0
for 0 based indices or 2
for 1-based
1 5 2
9 4 2
5 9 6
2
for 0 based or 10
for 1 based
0 3 0 4
0 4 1 4
4 3 1 2
-2 4 -2 -1
11
3 -1 3 3
3 -1 3 1
6
for 0 based or 3
for 1 based
Anecdote
Iverson's answer was ++/(M = ⍳¹ ⨢ ⍳¹)//M, which is neither valid in the Iverson Notation as defined in A Programming Language, nor in what eventually became APL. In Iverson notation, it would have been +/(M = ⍳¹(μ(M)) ⨢ ⍳¹(ν(M)))/M. In the very first versions of APL it was +/(,M=(⍳1↑⍴M)∘.+⍳1↓⍴M)/,M
.
in which Ken’s reply to Dijkstra’s question was a one-liner. But then that one-liner was wrong? – Luis Mendo – 2016-07-19T14:52:16.387
Do I need to output it or print it out, or can I just write down the expression as a snippet? – Leaky Nun – 2016-07-19T15:01:42.597
2@LuisMendo No, Iverson was actively designing the language, and at that iteration, his one-liner was correct. "APL" became famous with the publishing of the book *A Programming Language*, but at the time of publishing, the second expression was needed. Neither of these notations were ever implemented to be machine-executable. – Adám – 2016-07-19T15:04:30.517
@LeakyNun *Write a snippet or expression to calculate* – Adám – 2016-07-19T15:05:35.330
@Adám Thanks. It makes more sense now – Luis Mendo – 2016-07-19T15:06:57.557
Can I store the output in a variable? – Leaky Nun – 2016-07-19T15:44:41.893
@LeakyNun Yes you can. – Adám – 2016-07-19T16:09:55.117