I haven't tested this yet, but let me give you a lesson in lists.... see if you can work out the first problem I saw.
So LISP - LISt Processing.... it is designed around lists but get them wrong and odd things happen. Lists can be made up in a few ways:
A. (list ...... ) defines a list
B. (cons .....) defines a list
C. '(..... ) defines a list
C. creates a fixed list and what is inside is not evaluated '(1 2 (+ 2 3)) is 3 items 1, 2, (+ 2 3) (ignoring the error that (+ 2 3) should be a sting)
B. is mostly used in a dotted pair, such as (cons 0 "PolyLine") which might be used in say an ssget filter and is calculated, can be written as '(0 . "POLYLINE")
A. is a generic list function and also creates a list allowing for calculations inside: (list 1 2 (+ 2 3)) is a 3 item list: (1 2 4) - calculation done, can be '(1 2 4)
So in your function above if you have say a list like in C - fixed - and want to do a calculation or use a variable then it might go wrong
See if that helps and take the hint of the example in B, example in ssget.
Might be wrong, might not.