Group Abstract Group Abstract

Message Boards Message Boards

1
|
3.5K Views
|
6 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Protected error while using PrependTo at Flatten?

Posted 3 years ago

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.

POSTED BY: Zhenyu Zeng
6 Replies

(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]
POSTED BY: Daniel Lichtblau
Posted 3 years ago

Thanks for your reminder. It's my fault. I will check that.

POSTED BY: Zhenyu Zeng
Posted 3 years ago

Try Prepend instead of PrependTo

a = Prepend[Flatten[{{1, 2}}], {3}]
POSTED BY: Hans Milton
Posted 3 years ago

Okay. Thanks a lot.

POSTED BY: Zhenyu Zeng
Posted 3 years ago

If you are wondering why there is a difference, compare

Attributes@PrependTo
(* {HoldFirst, Protected} *)

Attributes@Prepend
(* {Protected} *)
POSTED BY: Rohit Namjoshi
Posted 3 years ago

How to understand HoldFirst and Protected? I see them in documentation, but they are too abstract.

POSTED BY: Zhenyu Zeng
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard