Syntax error when attempting to amend a string wit

2019-08-20 12:46发布

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?

2条回答
Summer. ? 凉城
2楼-- · 2019-08-20 13:17

That bracket notation is made specifically for modifying variables. The return value of T[1 2 3] ← 'COM' is 'COM', so if the expression didn't modify a variable, it would be pointless (or, almost identical to ).

To get a modified array, not modify a variable, use the operator @:

('COM'@1 2 3) 'computer'

Try it online!

查看更多
你好瞎i
3楼-- · 2019-08-20 13:18

'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!

查看更多
登录 后发表回答