more fun with lists

This is a moderated forum that collects tutorials, guides, and references for creating Transcendence extensions and scripts.
Post Reply
User avatar
Betelgeuse
Fleet Officer
Fleet Officer
Posts: 1920
Joined: Sun Mar 05, 2006 6:31 am

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?
Last edited by Betelgeuse on Thu Jun 26, 2008 7:29 am, edited 1 time in total.
Crying is not a proper retort!
User avatar
Periculi
Fleet Officer
Fleet Officer
Posts: 1282
Joined: Sat Oct 13, 2007 7:48 pm
Location: Necroposting in a forum near you

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]
User avatar
Betelgeuse
Fleet Officer
Fleet Officer
Posts: 1920
Joined: Sun Mar 05, 2006 6:31 am

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).
Crying is not a proper retort!
User avatar
digdug
Fleet Admiral
Fleet Admiral
Posts: 2620
Joined: Mon Oct 29, 2007 9:23 pm
Location: Decoding hieroglyphics on Tan-Ru-Dorem

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 ?
User avatar
Betelgeuse
Fleet Officer
Fleet Officer
Posts: 1920
Joined: Sun Mar 05, 2006 6:31 am

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?
Crying is not a proper retort!
User avatar
Betelgeuse
Fleet Officer
Fleet Officer
Posts: 1920
Joined: Sun Mar 05, 2006 6:31 am

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)))
Crying is not a proper retort!
F50
Fleet Officer
Fleet Officer
Posts: 1004
Joined: Sat Mar 11, 2006 5:25 pm

is there a nice way to save lists on the playership?
Last edited by F50 on Sat Sep 27, 2008 2:49 pm, edited 1 time in total.
User avatar
Betelgeuse
Fleet Officer
Fleet Officer
Posts: 1920
Joined: Sun Mar 05, 2006 6:31 am

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.
Crying is not a proper retort!
F50
Fleet Officer
Fleet Officer
Posts: 1004
Joined: Sat Mar 11, 2006 5:25 pm

Betelgeuse wrote: so it will not have the references.
Crap. So if I am trying to save a list of references I am screwed?
User avatar
Betelgeuse
Fleet Officer
Fleet Officer
Posts: 1920
Joined: Sun Mar 05, 2006 6:31 am

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.
Crying is not a proper retort!
Post Reply