a couple of helper functions for manipulating lists

This is a moderated forum that collects tutorials, guides, and references for creating Transcendence extensions and scripts.
Post Reply
User avatar
digdug
Fleet Admiral
Fleet Admiral
Posts: 2611
Joined: Mon Oct 29, 2007 9:23 pm
Location: Decoding hieroglyphics on Tan-Ru-Dorem

Wed Dec 17, 2008 7:25 pm

On IRC we noted that set theory operations could be very useful if applied to Transcendence Lists.

for example append will join two lists into one, but without any check on the elements of the lists, what if I want to join two lists without duplicate elements ? (this operation is called Union in set theory)
(more info on what I'm talking about is here http://en.wikipedia.org/wiki/Set_theory#Basic_concepts)

So me and Betel made a couple of functions:
the first one is Union
(intUnionList '(a b c) '(c d e)) Returns '(a b c d e)

the second one is Intersection, it will return a list with only the items in common between the 2 argument lists.
(intIntersectList '(a b c d) '(c d e f)) Returns '(c d)

Here the 2 functions:

Code: Select all

(setq intUnionList (lambda (list1 list2)
(block (FinalList)
(setq FinalList Nil)
(enum list1 theElement1
	(block (elementIndex)
		(setq elementIndex (find FinalList theElement1))
		(if (not elementIndex)
			(setq FinalList (lnkAppend FinalList theElement1))
		)
	)
)
(enum list2 theElement1
	(block (elementIndex)
		(setq elementIndex (find FinalList theElement1))
		(if (not elementIndex)
			(setq FinalList (lnkAppend FinalList theElement1))
		)
	)
)
FinalList
)))
;--------------------------------------------------------------------------------
(setq intIntersectList (lambda (list1 list2)
(block (FinalList)
(setq FinalList Nil)
(enum list1 theElement1
	(block (elementIndex)
		(setq elementIndex (find list2 theElement1))
		(if (not (eq elementIndex Nil))
			(setq FinalList (lnkAppend FinalList theElement1))	
		)
	)
)
FinalList
)))

What do you think ?

User avatar
Periculi
Fleet Officer
Fleet Officer
Posts: 1259
Joined: Sat Oct 13, 2007 7:48 pm
Location: By the beach

Thu Dec 18, 2008 6:09 am

sweet, and about time! Thanks. :)

Post Reply