[List]Two lists sharing data values

Report bugs. Post bug workarounds or fixes

Moderator: MSandro

Post Reply
SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

[List]Two lists sharing data values

Post by SimpleSi » Oct 30th, '17, 21:48

Similar (but simpler to demonstrate) to previous reported list bug

I don't think orig list should be affected by changes in current but it is being
Attachments
eulerProject24.gpp
(5.92 KiB) Downloaded 322 times

SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

Re: [List]Two lists sharing data values

Post by SimpleSi » Oct 31st, '17, 13:54

May have found a workaround
If I set current to flattened(orig)

then current becomes separated from orig

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: [List]Two lists sharing data values

Post by JohnM » Nov 5th, '17, 17:14

What's going on here is what computer scientists call "aliasing": two variables that refer to the exact same object (a list). This is not a bug and it is often useful, but it can be confusing.

As an analogy, if I called a cat "Ginger" and you called it "Furball" and I let "Ginger" out then you asked "Is Furball outside?" the answer would be "yes". "Furball" and "Ginger" are just different name for the same cat.

Similarly, in your eulerProject24, "orig" and "current" are just two names for the same list object. So, whatever you do to the list using the variable "current" will effect the list referred to by "orig", since its the exact same list.

As you discovered, what you want in this case is to make a copy of the original list. You can then change that copy without changing the original list.

As an another analogy, if I give you the original paper copy of a story I'm writing and you scribble all over it with a black marker, I might not be able to read what I originally wrote. But if I give you a photocopy of that story and keep the original, no matter what you do to the copy I still have the original.

As you discovered, "flattened" makes a copy of a list. The "copy" block in the same category does the same thing, and expresses the purpose more clearly to anyone reading the code.

SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

Re: [List]Two lists sharing data values

Post by SimpleSi » Nov 5th, '17, 18:51

aah

It never occurred to me that a list was considered to be an object

I will adjust my thinking :)

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: [List]Two lists sharing data values

Post by JohnM » Nov 5th, '17, 21:12

In GP, every piece of data -- that is, anything you can store in a variable -- is an object. That includes numbers and strings, colors, lists, dictionaries, tables, and, of course, instances of your own classes.

The aliasing phenomenon can only be observed if you can change the referenced object, and you can't change numbers or strings (in GP; some languages do let you change strings). Numbers and GP strings are examples of "immutable" objects. Since you can't change those objects, it doesn't matter if two variables refer to the same object; there's nothing you can do to the object through one variable that will make it change when observed via the other variable.

Scratch neatly avoids the aliasing issue by not allowing two variables to refer to a list. You can create a list variable, but you can't store a reference to that list into another variable. This is an excellent design choice for the Scratch audience; it allows beginners to learn about lists (already a complex thing to learn) without the possibility of unexpected aliasing.

However, as the Snap! folks point out, "first class lists" -- the ability to pass references to lists as function/method parameters, store them in variables, and store them into other lists -- is a powerful feature. It allows you to use lists-of-lists to represent tables, musical scores, folder hierarchies, and many other nifty, beautiful, and useful things. First class lists are a good choice for GP's target audience -- but using that feature does entail learning about aliasing. :-)

SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

Re: [List]Two lists sharing data values

Post by SimpleSi » Nov 6th, '17, 09:54

You learn something new everyday :)

Post Reply