Reputation: 843
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?
Upvotes: 1
Views: 185
Reputation: 7616
,
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∘,¨
.
Upvotes: 1
Reputation: 701
1 2 3,¨4 5 6
Catenate each element of 1 2 3
with each element of 4 5 6
1 2 3∘,¨4 5 6
Catenate 1 2 3
with each element of 4 5 6
(2 2⍴⍳4),¨(2 2⍴⎕A)
┌───┬───┐
│1 A│2 B│
├───┼───┤
│3 C│4 D│
└───┴───┘
(2 2⍴⍳4)∘,¨(2 2⍴⎕A)
┌─────┬─────┐
│1 2 A│1 2 B│
│3 4 A│3 4 B│
├─────┼─────┤
│1 2 C│1 2 D│
│3 4 C│3 4 D│
└─────┴─────┘
Upvotes: 1