Pick with Zilde as an argument

General APL language issues

Pick with Zilde as an argument

Postby alexeyv on Thu Jan 05, 2017 2:03 pm

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⊃A
Y

While the code
Code: Select all
      3 2⊃A
RANK 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: 44
Joined: Tue Nov 17, 2015 4:18 pm

Re: Pick with Zilde as an argument

Postby JohnS|Dyalog on Thu Jan 05, 2017 6:33 pm

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 as
world
(,2)⊃'hello' 'world' ⍝ a 1-vector path with a scalar item, which is
world
(,⊂,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-path
1
'xyz' ≡ (⊂⍳0)⊃s ⍝ (⊂⍳0) a 1-path with one 0-item
1

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
-
User avatar
JohnS|Dyalog
 
Posts: 145
Joined: Wed Sep 10, 2008 10:01 am

Re: Pick with Zilde as an argument

Postby alexeyv on Fri Jan 06, 2017 6:38 pm

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: 44
Joined: Tue Nov 17, 2015 4:18 pm


Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest