VCAT: A Response
Vector 6:3, page 100
Transcribed from the printed issue; not yet reviewed. Read it in the PDF of the issue, page 100
While it gives me great pleasure to see Peter Branson’s contribution to the Education Vector (Vol.6 No.2) I wonder whether his VCAT example reflects the ways in which we should be encouraging APL newcomers to tackle such a problem now that APL2 is becoming so commonplace that even kids can have it for nothing (providing of course that they have access to a computer made by a certain well-known manufacturer...!)
The modern didactic view of this situation is to make each of the two matrices into vectors of the rows (⊂[2]¨), join them (,/), then restore the second dimension by de-nesting (⊃).
Here is the function:
[0] Z←L VCAT R
[1] Z←⊃⊃,/⊂[2]¨L R
LEFT RIGHT
BREAD MAN
FRUIT CAN
EAT
LEFT VCAT RIGHT
BREAD
FRUIT
MAN
CAN
EAT
The second disclose is necessary on account of the implicit enclosure arising from the derived function ,/.
This VCAT is fine provided both arguments are matrices. If the function is to work with vectors or scalars, we have to generalise ⊂[2] to:
1/R which makes R into a 1-item vector if it is a scalar, otherwise does nothing.
Rewriting VCAT we have:
[0] Z←L VCAT R
[1] Z←⊃⊃,/EXPAND¨L R
LEFT VCAT '3'
BREAD
FRUIT
3
3 VCAT 4
3
4
'PIG' VCAT 'SHEEP'
PIG
SHEEP
Norman Thomson
IBM UK Laboratories
Ed: this looked interesting enough to time! From the results below, I would have to say that I side with Peter, at least until someone comes up with an interpreter that will run Norman’s code as fast for realistic sizes of data!
+-----------------------------------------+
| | | Z←L VCAT R |
| ⍴LEFT | ⍴RIGHT | NT | PB |
| | | VCAT | VCAT |
|---------|---------|----------|----------|
| 2 5 | 3 3 | 270 | 380 |
| 12 80 | 23 | 1480 | 390 | ⍝ screen builder
| 56 132 | 63 | 9500 | 450 | ⍝ report builder
+-----------------------------------------+