## Pick with Zilde as an argument

General APL language issues

### Pick with Zilde as an argument

Hi all,

While studying "APL 2 At A Glance" I've encountered the following exercise which I couldn't understand.
Given vector
Code: Select all
`A←(⍳4)(2 3⍴'ABCDEF')(⊂'XYZ')(6 (7 8))`

I'm trying to understand the following statement:
Code: Select all
`      3(⍳0)2⊃AY`

While the code
Code: Select all
`      3 2⊃ARANK ERROR`

produces the error.
From what I can understand 3 should give us the 3rd element of the vector, which is enclosed array 'XYZ'. So the error in second case is understandable - we can't get the value from the scalar.
Why then the ⍳0 in the middle could help?
Running it separately:
Code: Select all
`      (⍳0)⊃3⊃A┌───────┐│ ┌→──┐ ││ │XYZ│ ││ └───┘ │└∊──────┘`

so it is not removing the scalar enclosure.
alexeyv

Posts: 56
Joined: Tue Nov 17, 2015 4:18 pm

### Re: Pick with Zilde as an argument

The left argument of pick is a vector, which defines a "path" through the (typically nested) right argument array.

Each item in the path is a vector, whose shape matches the rank of the right argument item into which we're picking: a 2-vector item picks into a matrix; a 1-vector item picks into a vector and a 0-vector item picks into a scalar.

Pick is permissive on two counts:
- A scalar path is interpreted as a 1-vector path
- A scalar path-item is interpreted as a 1-vector path-item
so:
`      2⊃'hello' 'world'       ⍝ A scalar path is interpreted asworld      (,2)⊃'hello' 'world'    ⍝ a 1-vector path with a scalar item, which isworld      (,⊂,2)⊃'hello' 'world'  ⍝ interpreted as a 1-vector item.world`

Note that an empty (0-vector) path does zero picking, while a path with one empty (0-vector) item picks into a scalar:

`      s ← ⊂'xyz'       ⍝ s is scalar      s ≡ (⍳0)⊃s       ⍝ (⍳0) is a 0-path1      'xyz' ≡ (⊂⍳0)⊃s  ⍝ (⊂⍳0) a 1-path with one 0-item1`

User command ]display is our friend when playing with pick. Each item in the path penetrates one border:

`      ]display hello ← ⊂⊂1 1⍴⊂⊂'hello'┌─────────────────────┐│ ┌─────────────────┐ ││ │ ┌→────────────┐ │ ││ │ ↓ ┌─────────┐ │ │ ││ │ │ │ ┌→────┐ │ │ │ ││ │ │ │ │hello│ │ │ │ ││ │ │ │ └─────┘ │ │ │ ││ │ │ └∊────────┘ │ │ ││ │ └∊────────────┘ │ ││ └∊────────────────┘ │└∊────────────────────┘      ]display ⍬ ⍬ ⊃ hello┌→────────────┐↓ ┌─────────┐ ││ │ ┌→────┐ │ ││ │ │hello│ │ ││ │ └─────┘ │ ││ └∊────────┘ │└∊────────────┘      ]display ⍬ ⍬(1 1) ⊃ hello┌─────────┐│ ┌→────┐ ││ │hello│ ││ └─────┘ │└∊────────┘      ]display ⍬ ⍬(1 1)⍬ ⊃ hello┌→────┐│hello│└─────┘      ]display ⍬ ⍬(1 1)⍬ 2 ⊃ hello e-`
JohnS|Dyalog

### Re: Pick with Zilde as an argument

Thanks for the detailed explanations! I guess the
a 2-vector item picks into a matrix; a 1-vector item picks into a vector and a 0-vector item picks into a scalar.

is the actual answer to my question. Now it is more clear.
alexeyv

Posts: 56
Joined: Tue Nov 17, 2015 4:18 pm