Search code examples
indexingvariable-assignmentin-placeapldyalog

Syntax error when attempting to amend a string with indexing


I'm studying APL from here.

Why am I getting this syntax error?

      'computer' [ 1 2 3 ] ← 'COM'
SYNTAX ERROR
'computer'[1 2 3]←'COM'
          ^

But if I save 'computer' in a variable I don't get the error:

      T ← 'computer'
      T
computer
      T[1 2 3] ← 'COM'
      T
COMputer

What am I doing wrong?


Solution

  • 'computer' is a constant, and you can't change the value of a constant itself, only the current value of a variable.

    Think about it: If you could assign to 'computer', then next time you wrote 'computer', would you expect the result to be COMputer? How about 2←3? Clearly, this doesn't make any sense.

    However, you can amend a value without assigning it to a name, using the relatively new @ "at" operator (it isn't included in Mastering Dyalog APL, but the documentation is available online).

          'COM'@1 2 3⊢'computer'
    COMputer
    

    You can read this as put the letters 'COM' at indices 1 2 3 of the word 'computer'. The here only serves to separate 1 2 3 from 'computer so it is clear to @ what constitutes the indices and what is the array to be amended.

    Run it on TryAPL!