Friday, July 15, 2016

J provokes self-talk (3)

I can't really postpone talking about J's data structure(s). Inevitably, they are crucial. As I start to understand them, they seem fairly simple - but I've beaten my head against a few walls to get this far. Starting with 0{t12, which returns a row of an array, and (13|t1){'AKQJT98765432', which returns characters from a string. Que?

Lists are the (maybe 'a') fundamental data structure. They are so fundamental that for a numeric list, the sole delimiter is a blank space.  2 3 6 7 is a list. It takes a while to get used to this, if like me you come from a universe of list = ({[,,,,...}}). I'm not criticising - I'm impressed by the commitment to terseness. But it does take a while. Also, a string is a list (of characters) and likewise an array is a list (of lists). Lists have shape ($).

$ 'string'
6                  The shape of a string (or any one dimensional list) is its length.
$ t11

4 13             The shape of a list of lists (do NOT think about arrays) is rows * columns.

{ is called "from" and its right hand side, its y input, is the source while the left hand side -  x input - is an index.  A scalar index (a simple number) operates on the leftmost dimension of the shape (zero-based), so it returns the nth element of that dimension. In a list of list of lists, it would return a list of lists.

$ threed
2 3 4             a list of list of lists
threed
0  1  2   3
4  5  6   7
8  9 10 11 returns a 3*4 list of lists.

{ can be indexed by a list, as well as a scalar. But you have to box the indexing list:
0 1 t11
A9242389TA786
JT267QK9T5AQK      unboxed, it returns row 0 and row 1

(<0 1) { t11
9                                   boxed, it returns the 1th element of the 0th row.

There are a lot of index structures - since the list of lists is pretty much the only structure, there need to be. But for now, that will do.

This 'sentence' (The J language metaphor drives me crazy, but it seems to make everybody else happy, and community rules, OK?) gives the first insight into how J handles iteration (1).
0 1 t11
A9242389TA786
JT267QK9T5AQK      unboxed, it returns row 0 and row 1.

Why? Because this is one of the iteration patterns, in effect;
FOR index_number IN (0,1)
     RETURN t11(index_number)
     END
'Boxing' the 0 1 turns off the iteration, and allows them/it to be used as a single index value. Built into the J website is a book (it's a book book as well, if you like that kind of thing) called 'cforj' (why not 'jforc' I can only put down to mysteries of dialect) by someone so modest that I haven't fathomed their name yet. A pity - I don't write C and it was still extremely helpful. This author devotes 6 scattered chapters to iteration, in increasing levels of complexity. Worth the effort. Have a recommend!

(1) J has a perfectly respectable set of imperative style 'verbs'; do, if, while &c. But I didn't start J to practise my imperative programming, so they'll have to wait.


No comments:

Post a Comment