Page 1 of 1
more fun with lists
Posted: Thu Jun 19, 2008 3:48 am
by Betelgeuse
lists are not like numbers and strings they are more like objects in that things that effect them can effect many different variables (like adding an item to a ship makes all references to that ship have that item)
Code: Select all
(setq test (lambda nil
(block (mylist copyList result)
(setq mylist '((a b c)))
(setq copyList '((d e f)))
(setq result (append mylist copyList))
(lnkRemove (item result 0) 0 nil)
mylist
)
))
This code returns the list ((b c)) even though mylist was never directly referenced. Lists made up of the original list can still effect the original list.
Code: Select all
(setq test2 (lambda nil
(block (mylist copyList result)
(setq mylist '((a b c)))
(setq copyList '((d e f)))
(setq result (append mylist copyList))
(lnkRemove (item mylist 0) 0 nil)
result
)
))
This code return the list ((b c) (d e f)) even though result was never changed. So that shows that the lists that make up a list can effect it after its made.
Code: Select all
(setq test3 (lambda nil
(block (mylist copyList result)
(setq mylist '((a b c)))
(setq copyList '((d e f)))
(setq result (append mylist copyList))
(setq mylist '((g h i)))
result
)
))
This code return the list ((a b c) (d e f)). This shows that like space objects setting the variable to a different space object doesn't change the original or derived list.
and one more just to confuse you
Code: Select all
(setq test4 (lambda nil
(block (mylist copyList result)
(setq mylist '(a b c))
(setq copyList '(d e f))
(setq result (append mylist copyList))
(lnkRemove result 0 nil)
mylist
)
))
This will return the list (a b c). This is due to how append works. Append makes a new list with the top level elements of both the arguments. So you don't have to worry about things changing the top level of the list just the sub lists. (please ask questions about this I am not sure how to describe this better)
any questions or comments?
Posted: Thu Jun 26, 2008 5:27 am
by Periculi
In test 4 you return mylist. So, of course it returns '(a b c). LoL! And it IS NOT due to how append works.
This function does the same thing:
(setq test4 (lambda nil
(block (myList)
(setq myList '(a b c))
myList
)
))
Shouldn't you be returning result?
I mean, that is what the name of the appended sample list is.. right?
result should be something like '(b c d e f)
Or is your point that myList isn't changed by result list getting item 0 removed?
[rant]
Also, those are not strings- "a" "b" "c". Samples like that always confuse me, because it would really be better to demonstrate with strings for us lowly noobs. In your examples a could be "A" or it could be "Aye" or it could be 1 or True or "This is the string value of variable A." or even a LIST '(z y x).
[/rant]
Posted: Thu Jun 26, 2008 7:27 am
by Betelgeuse
Periculi wrote:
Or is your point that myList isn't changed by result list getting item 0 removed?
That is exactly the point. Look again at the first test and compare it to the fourth test. They are both acting on a but due to the way append works four doesn't remove it from mylist and the first test does. (I edited it to correct the output. Test one returns ((b c)) forgot about that when typing it in.
Periculi wrote:
[rant]
Also, those are not strings- "a" "b" "c". Samples like that always confuse me, because it would really be better to demonstrate with strings for us lowly noobs. In your examples a could be "A" or it could be "Aye" or it could be 1 or True or "This is the string value of variable A." or even a LIST '(z y x).
[/rant]
I didn't want them to be strings. Strings are atoms and you don't know if a b and c are atoms or not. (unless you are working with a specified format)
Let me put this a different way.
Lets say we have an element A. (you don't know whats in it)
List B contains element A.
List D contains element A.
List C contains element B. (remember a list can be an element too)
Any change to element A would change all four of them if A is a list.
Certain changes to the list C can change element A or List B or List D. (not necessarily with the same function)
Certain changes to the list B can change element A or List C or List D. (not necessarily with the same function)
This can go on forever, you can have a list that has an element 20 lists down be shared with a different list constructed at a different time, and a change with a list that has little to do with your list can change it unless you copy the list (see my list copy function).
Posted: Thu Jun 26, 2008 1:26 pm
by digdug
I still don-t get it completely
MyList in the first example is '((a b c)), so it's a list within a list?
Result list contains 2 lists '((a b c) (d e f)), right ?
While example four
MyList is '(a b c), so it's a list with 3 elements inside.
and Result is a list with 6 elements ? '(a b c d e f)
As far as I can understand, from what you get as output, it means that only lists that contain lists are recursively connected (hope it's right) ?
What about lists that contain mixed elements, atoms and not atoms ?
Posted: Thu Jun 26, 2008 10:08 pm
by Betelgeuse
in the case of mixed atoms and non atoms only changes in the non atoms will effect the current list but changes in both the atoms and non atoms can effect other lists.
here is an example of mixed atoms and non atoms plus demonstrates a couple more points (a change in one part of the list can effect other parts of the same list and that identical lists can be different and sometimes you don't know if you are building lists of lists)
Also found out that append doesn't append nil.
Code: Select all
(setq test5 (lambda (unknown)
(block (mylist copyList result)
(setq otherUnknown '((X Y)))
(setq builderList (append unknown "b" "c"))
(setq otherList (append unknown "b" "c" otherUnknown))
(setq mylist (append (list builderList) (list builderList) "this is a string" (list otherList)))
(setq copyList '((d e f)))
(setq result (append mylist copyList))
(lnkRemove (item result 0) 0 nil)
mylist
)
))
a unknown of "a" would give you the mylist ((b c) (b c) "this is a string" (a b c (X Y)))
a unknown of ((X Y)) would give you the mylist ((b c) (b c) "this is a string" ((X Y) b c (X Y)))
a unknown of nil would give you the mylist ((c) (c) "this is a string" (b c (X Y)))
a unknown of (q w) would give you the mylist ((w b c) (w b c) "this is a string" (q w b c (X Y)))
any questions?
Posted: Thu Jun 26, 2008 11:54 pm
by Betelgeuse
a bonus example
Code: Select all
(setq test6 (lambda (unknown)
(block (mylist copyList result)
(setq otherUnknown '((X Y)))
(setq builderList (append unknown "b" "c"))
(setq otherList (append unknown "b" "c" otherUnknown))
(setq mylist (append (list builderList) (list builderList) "this is a string" (list otherList)))
(setq copyList '((d e f)))
(setq result (append mylist copyList))
(lnkRemove (item (item result 0) 0) 0 nil)
mylist
)
))
nearly the same code as test5 but going down one more level
an unknown of ((x y) (w z)) gives a mylist of (((y) (w z) b c) ((y) (w z) b c) "this is a string" ((y) (w z) b c (X Y)))
Posted: Fri Sep 26, 2008 11:08 pm
by F50
is there a nice way to save lists on the playership?
Posted: Fri Sep 26, 2008 11:16 pm
by Betelgeuse
you can save a list that just has things you would normally save in a ship variable. This will just be a copy of the list so it will not have the references.
Posted: Sat Sep 27, 2008 2:49 pm
by F50
Betelgeuse wrote: so it will not have the references.
Crap. So if I am trying to save a list of references I am screwed?
Posted: Sat Sep 27, 2008 6:23 pm
by Betelgeuse
F50 wrote:Betelgeuse wrote: so it will not have the references.
Crap. So if I am trying to save a list of references I am screwed?
basically yes. You have to save the original information or use something that is designed to store them. (for example ships have objID and you can also store them with RefData) You can not currently save the linking that lists do with the lnk functions.