尝试使用索引修改字符串时的语法错误 [英] Syntax error when attempting to amend a string with indexing
问题描述
我正在从此处学习APL.
为什么会出现此语法错误?
Why am I getting this syntax error?
'computer' [ 1 2 3 ] ← 'COM'
SYNTAX ERROR
'computer'[1 2 3]←'COM'
^
但是,如果我将'computer'
保存在变量中,则不会收到错误消息:
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
我在做什么错了?
推荐答案
'computer'
是常量,您不能更改常量本身的值,只能更改变量的当前值.
'computer'
is a constant, and you can't change the value of a constant itself, only the current value of a variable.
考虑一下:如果您可以分配给'computer'
,那么下次编写'computer'
时,您希望结果为COMputer
吗? 2←3
怎么样?显然,这没有任何意义.
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.
但是,您可以使用相对较新的@
"at"运算符(不包含在 Mastering Dyalog APL中,修改值而不将其分配给名称). em>,但该文档可在线获得)
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
您可以在在计算机"一词的第1 2 3个索引中输入字母'COM'来阅读它..此处的⊢
仅用于将1 2 3
与'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.
这篇关于尝试使用索引修改字符串时的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!