Search code examples
arraysmap-functionapldyalog

Semantics of x,¨y in APL: ie, what does it mean to map a catenate in APL?


Consider the expression (1 2 3),¨(4 5 6). I expected this to "map the operation (1 2 3), on each of 4, 5, and 6, giving the answer as:

(1 2 3),¨(4 5 6)
= (1 2 3),¨((4) (5) (6)) [Using (x) = x]
= (((1 2 3), 4) ((1 2 3), 5) ((1 2 3), 6)) [Using definition of map]
= ((1 2 3 4) (1 2 3 5) (1 2 3 6))

However,this is not the answer! The answer as evaluated in Dyalog APL is:

     ]display (1 2 3),¨(4 5 6) 
┌→──────────────────┐
│ ┌→──┐ ┌→──┐ ┌→──┐ │
│ │1 4│ │2 5│ │3 6│ │
│ └~──┘ └~──┘ └~──┘ │
└∊──────────────────┘

How? What's the reasoning behind this answer? Where did I go wrong in my equational reasoning? Are there more "gotchas" that my incorrect mental model of , (comma) and ¨(map) that I should be aware of?


Solution

  • , is a symmetric function, it simply concatenates its arguments.

    ¨ is also symmetric, it pairs up elements from left and and right.

    According to APL's scalar extension rules, a single element as argument gets distributed to pair up with all the elements from the other argument.

    You speak of the operation (1 2 3), but there is no such operation. If you try to give this "function" a name, it'll fail with a SYNTAX ERROR.

    However, you can create a function which takes its argument and appends it to 1 2 3 as Richard Park demonstrated; 1 2 3∘, and you can then map that function over the elements of an array with 1 2 3∘,¨.