Basic question about memory allocation

Thanks for bearing with me :slight_smile:

Well, here is an example of an implementation of chained lists (as an exercise!), where everything is immutable, in a Pluto notebook. And here is a version with mutable structs, in a Jupyter notebook.

In the immutable version, I think that if L is a chained list and I go NonEmplyList(a, L) (or a --> L), to add an element at the beginning of the list, then L gets copied. I’d like to know whether this is the case, and more importantly, I’d like to know why, and how I could have predicted the behaviour.

If the answer is “it depends”, then well, I’m uncomfortable.