Page 1 of 1

a couple of helper functions for manipulating lists

Posted: Wed Dec 17, 2008 7:25 pm
by digdug
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 ?

Posted: Thu Dec 18, 2008 6:09 am
by Periculi
sweet, and about time! Thanks. :)