4

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

Harley
  • 83
  • 7
  • Get an introductory APL textbook like Gilman and Rose and work through all of the examples of indexing of simple (that is, not nested) objects. It is vital that you understand the role of the shapes of the arguments, such as vector[scalar], vector[vector], matrix[scalar;scalar], how they affect the result, and how they relate to what you know from C (i.e. vector[scalar]). Once you have mastered the basics, only then move on to nested array examples such as (17 4711 (42 666)). Gilman and Rose was written well before the advent of nested arrays, so there will be no nested examples there. – Lobachevsky Oct 19 '17 at 05:34
  • 1
    Interesting choice of numbers, (42 666) – Lobachevsky Oct 19 '17 at 05:37
  • I initially had two questions, but then realized that the data type between [ and ] was important (that [0] and [,0] is giving different results). So now, I only seem to have one remaining problem. I will also look for the Recommended books. Thanks ! – Harley Oct 19 '17 at 09:24
  • 2
    While I agree with the idea of reading a good book on APL, I definitely want to recommend "Mastering Dyalog APL" over G&R. Simply because G&R, which I hold in high esteem because I learned APL with it, is outdated now. Mastering Dyalog APL can be freely downloaded from https://www.dyalog.com/mastering-dyalog-apl.htm – MBaas Oct 19 '17 at 09:35
  • @MBaas is that book applicable to ISO APL as well? Are the Dyalog extensions called out as such? I'm learning APL but am not sure yet I want to commit to Dyalog. – Daniel Lyons Oct 20 '17 at 15:32
  • @DanielLyons : I'm not sure really. The last time I used anything other than Dyalog was in 1994 - and I never looked back. – MBaas Oct 20 '17 at 15:34
  • @Harley can you pls. accept Adám's answer (click the checkmark-icon next to the answer), so that the question is no longer shown as "open"... – MBaas Oct 21 '17 at 11:47
  • @DanielLyons Mastering Dyalog APL was written for version 12.0 which was basically ISO APL. Only from 14.0 and onwards did Dyalog add significant extensions to the language. – Adám Oct 22 '17 at 15:25
  • Thanks, all ! I am still studying APL (especially Dyalog), but also have focus on some other activities. I really like that the "Mastering Dyalog APL" book exists. I have read most parts of it once. It was unexpected to find important information about data types several hundred pages in into the book. I was not aware of the existens of scalars encapsulating complex data types as arrays and the enclose/disclose functions. It was a suprise that such information was not early in the book, even if nested arrays was added to the language after some time. – Harley Jan 03 '18 at 18:38

1 Answers1

3

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!

Adám
  • 6,573
  • 20
  • 37