xpqz / learnapl

Introduction to Dyalog APL: https://xpqz.github.io/learnapl
Other
126 stars 11 forks source link

Sane indexing last sample doesnt work? #21

Closed tyoc213 closed 2 years ago

tyoc213 commented 2 years ago

This is sample session showing the error

Dyalog APL/S-64 Version 18.0.40684
Serial number: UNREGISTERED - not for commercial use
+-----------------------------------------------------------------+
| Dyalog is free for non-commercial use but is not free software. |
| A non-commercial licence can be used for experiments and        |
| proof of concept until the point in time that it is of value.   |
| For further information visit                                   |
| https://www.dyalog.com/prices-and-licences.htm                  |
+-----------------------------------------------------------------+
Wed Jan 26 22:30:45 2022

      I←⌷⍨∘⊃⍨⍤0 99 ⍝ Sane indexing

      m ← 3 3⍴(1 2 3)(3 2 1)(4 5 6)(5 3 1)(5 6 8)(7 1 2)(4 3 9)(3 7 6)(4 5 1)

      (0 0)(1 2)(2 2)I m ⍝ Multiple cells by index, like m[(0 0)(1 2)(2 2)]
INDEX ERROR
      (0 0)(1 2)(2 2)I m ⍝ Multiple cells by index, like m[(0 0)(1 2)(2 2)]
                     ∧
      I
   ⌷⍨ ∘⊃ ⍨ ⍤ 0 99 

Also this one in Reach indexing

      G[⊂(0 1)0]
INDEX ERROR
      G[⊂(0 1)0]
       ∧

      G[((0 0)0)((1 2)1)]
INDEX ERROR
      G[((0 0)0)((1 2)1)]
       ∧

And this Assignable indexing expressions

]DISPLAY m ← 3 3⍴9?9
┌→──────┐
↓┌→────┐│
│↓4 8 2││
││3 6 1││
││5 9 7││
│└~────┘│
└───────┘
(0 0⍉m) ← ¯1 ¯1 ¯1 ⍝ 0 0⍉m is the main diagonal.
DOMAIN ERROR
      (0 0⍉m)←¯1 ¯1 ¯1 ⍝ 0 0⍉m is the main diagonal.
xpqz commented 2 years ago

You're missing the crucial ⎕IO←0. See the first code cell at the top of every chapter. The Indexing chapter intro emphasises this:

But as before, we begin by setting our index origin, extra important as we’re about to discuss indexing.

     ⎕IO←0
      I←⌷⍨∘⊃⍨⍤0 99 
      m ← 3 3⍴(1 2 3)(3 2 1)(4 5 6)(5 3 1)(5 6 8)(7 1 2)(4 3 9)(3 7 6)(4 5 1)

      (0 0)(1 2)(2 2)I m
┌→────────────────────────┐
│ ┌→────┐ ┌→────┐ ┌→────┐ │
│ │1 2 3│ │7 1 2│ │4 5 1│ │
│ └~────┘ └~────┘ └~────┘ │
└∊────────────────────────┘
xpqz commented 2 years ago
      ⎕IO←0
      ]DISPLAY m ← 3 3⍴9?9
┌→──────┐
↓┌→────┐│
│↓0 3 5││
││4 2 1││
││7 6 8││
│└~────┘│
└───────┘
      (0 0⍉m) ← ¯1 ¯1 ¯1
      m
┌→───────┐
↓¯1  3  5│
│ 4 ¯1  1│
│ 7  6 ¯1│
└~───────┘
xpqz commented 2 years ago

⎕IO←0 is used exclusively in the book, reasons for which are explained in the first chapter.

tyoc213 commented 2 years ago

gotcha @xpqz thx, interesting, still didnt get why that was there, maybe skim a lot but could you explain what actually is doing and why it is needed?

For me not being English my first lang, isnt clear why I need to define that (except that doesnt work if I dont do that)... also why it isn't defined in the langauge itself or if there are other ways to define the base index. Or how it is referenecd used by other symbols that are already defined.

I think would be helpful to know why.

Anyway, thanks for the book, still searching more ways to learn about APL, resources seem scarse.

tyoc213 commented 2 years ago

I think I have it now, ⎕IO prints 1 just starting the repl, so setting it as 0 as "normal" programming.

So I guess that is a variable already defined on system, interesting way of APL to allow changing base index I guess.

Still dont know why I can't use IO←0 but must use Quad to set it or display it.

xpqz commented 2 years ago

That's right; APL allows you to set the "index origin", ⎕IO. The default is 1, which means that the first element of a vector is V[1].

For a learner, this is most likely confusing, as you probably know python or javascript or C or C++ or Java etc, so in this book we always start with setting ⎕IO←0, meaning that all vector and array indexing start from 0, not 1.

As you point out, it's an "interesting" choice that APL allows the user to change the index origin -- this will sooner or later cause a problem for every APL programmer one way or the other.

I'm in the process of publishing more APL material, by the way. Check it out: https://xpqz.github.io/cultivations

Thanks

Stefan

On Fri, 28 Jan 2022 at 06:34, tyoc213 (David L) @.***> wrote:

I think I have it now, ⎕IO prints 1 just starting the repl, so setting it as 0 as "normal" programming.

So I guess that is a variable already defined on system, interesting way of APL to allow changing base index I guess.

— Reply to this email directly, view it on GitHub https://github.com/xpqz/learnapl/issues/21#issuecomment-1023927979, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAUQ6JEDRORVYOCUFWBTHDLUYI2GVANCNFSM5M42VYVA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

-- Stefan Kruger @.***>