-->

Problems when trying to use arrays in APL. What ha

2019-07-10 03:00发布

问题:

I obviously have missed some things about how to extract elements from arrays in APL and hope that someone can see what I have missed and how I should do to get the expected results in a way that I can reproduce in a meaningful way.

I am relatively new in learning APL and I am more used to languages like Python and C. The data types and array manipulating tools in APL seem to confuse me, a little.

Consider the following code and please tell why the expected (by me) result,

┌→─────┐
│42 666│
└~─────┘

got embedded in something more complex, and possibly a way around that problem. (Using Dyalog APL/S-64, 16.0.30320)

      ⎕io ← 0
      a ← 17 4711 (42 666)
      z ← a[2]

      an_expected_vector←42 666
      ]DISPLAY an_expected_vector
┌→─────┐
│42 666│
└~─────┘

      ]DISPLAY z
┌──────────┐
│ ┌→─────┐ │
│ │42 666│ │
│ └~─────┘ │
└∊─────────┘

Why isn't z identical to an_expected_vector ?

Thanks ! /Hans

回答1:

2 is a scalar and so a[2] returns a scalar, which happens to be the vector 42 666. It is therefore enclosed in a level of nesting.

If you use the Pick function (dyadic ) you will get the expected result, as will pick the element indicated by the left argument, from the right argument:

       ⎕io ← 0
       a ← 17 4711 (42 666)
       z ← 2⊃a
       an_expected_vector ← 42 666
       z ≡ an_expected_vector
 1

Try it online!