Hello,
I run these codes and see an error:
a = PrependTo[Flatten[{{1, 2}}], {3}]
The error is:
Tag Flatten in Flatten[{{1,2}}] is Protected.
It is weird.
Hello,
I run these codes and see an error:
a = PrependTo[Flatten[{{1, 2}}], {3}]
The error is:
Tag Flatten in Flatten[{{1,2}}] is Protected.
It is weird.
Try Prepend instead of PrependTo
a = Prepend[Flatten[{{1, 2}}], {3}]
Okay. Thanks a lot.
If you are wondering why there is a difference, compare
Attributes@PrependTo
(* {HoldFirst, Protected} *)
Attributes@Prepend
(* {Protected} *)
How to understand HoldFirst and Protected? I see them in documentation, but they are too abstract.
(1) This forum is not intended to be a replacement for reading relevant documentation. It seems quite unlikely that you checked Help > Wolfram Documentation > PrependTo. And that is a prerequisite for posting a question like this.
(2) The documentation for PrependTo is quite clear on why the first argument needs to be something that can take an assignment. You cannot assign a value to Flatten[{{1, 2}}].
(3) It might be easier to understand this related example.
In[3]:= PrependTo[a, 3]
During evaluation of In[3]:= PrependTo::rvalue: a is not a variable with a value, so its value cannot be changed.
Out[3]= PrependTo[a, 3]
Thanks for your reminder. It’s my fault. I will check that.