Author Topic: member function with lists in lists  (Read 3377 times)

0 Members and 1 Guest are viewing this topic.

Amsterdammed

  • Guest
member function with lists in lists
« on: April 22, 2006, 11:32:06 AM »
Hello there,
i have  some list built up and i want add list to this one , but only if they are not allready member of the list. So i want to check out with the member function.

But why does
Code: [Select]

(if (not (member xfs axfs))
     (setq axfs (append axfs (list xfs))))





give nil if xfs =
Code: [Select]

 (150.0 250.0 400.0 500.0 735.0 1250.0)



and axfs =
Code: [Select]
((735.0) (150.0 250.0 400.0 500.0 735.0 1250.0))


?
 What do i overlook?

Thanks in Advance,
Bernd


MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: member function with lists in lists
« Reply #1 on: April 22, 2006, 01:09:22 PM »
If item order doesn't matter perhaps --

Code: [Select]
(defun MergeLists ( list1 list2 / _MergeLists )
    (defun _MergeLists ( list1 list2 )
        (foreach item list1
            (if (null (member item list2))
                (setq list2 (cons item list2))
            )
        )
        list2
    )
    (if (<= (length list1) (length list2))
        (_MergeLists list1 list2)
        (_MergeLists list2 list1)
    )
)

Example --

(MergeLists '(1 2 3) '(4 5 6))  >>>  (3 2 1 4 5 6)


If item order matters perhaps --

Code: [Select]
(defun MergeLists ( list1 list2 / _MergeLists )
    (defun _MergeLists ( list1 list2 )
        (foreach item (reverse list1)
            (if (null (member item list2))
                (setq list2 (cons item list2))
            )
        )
        list2
    )
    (if (<= (length list1) (length list2))
        (_MergeLists list1 list2)
        (_MergeLists list2 list1)
    )
)

Example --

(MergeLists '(1 2 3) '(4 5 6))  >>>  (1 2 3 4 5 6)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: member function with lists in lists
« Reply #2 on: April 22, 2006, 01:34:15 PM »
Given that you may have n number of lists to merge --

Code: [Select]
(defun MergeLists ( lists / _MergeLists )
    (defun _MergeLists ( list1 list2 )
        (append       
            (vl-remove-if
               '(lambda (item) (member item list2))
                list1
            )
            list2
        )   
    )
    (   (lambda ( result )
            (foreach lst (cdr lists)
                (setq result
                    (if (<= (length lst) (length result))
                        (_MergeLists lst result)
                        (_MergeLists result lst)
                    )
                )   
            )   
        )
        (car (setq lists (reverse lists)))
    )       
)

Code: [Select]
(MergeLists
   '(
        (1 2 3)
        (1 2 3)
        (4 5 6)
    )
)

>>> (1 2 3 4 5 6)

Note: Do not quote existing lists. i.e.

Code: [Select]
(setq
    list1 '(1 2 3)
    list2 '(A B C)
)

Correct: (MergeLists (list list1 list2)) >>> (1 2 3 A B C)

Wrong: (MergeLists '(list1 list2)) >>> Error


Edit: If you are h3ll bent on using '(list1 list2) because you think it looks cleaner you could always use mapcar / eval --

(MergeLists (mapcar 'eval '(list1 list2))) >>> (1 2 3 A B C)

But I wouldn't go that route myself.
« Last Edit: April 22, 2006, 01:43:51 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: member function with lists in lists
« Reply #3 on: April 22, 2006, 01:53:51 PM »
Hey Bernd --

If this is all overkill to you just  use the core technique --

Set some data --

Code: [Select]
(setq
    list1 '(1 2 3 4 5)
    list2 '(4 5 6 7 8)
)

Use it --

Code: [Select]
(setq result
    (append       
        (vl-remove-if
           '(lambda (item) (member item list2))
            list1
        )
        list2
    )
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Amsterdammed

  • Guest
Re: member function with lists in lists
« Reply #4 on: April 22, 2006, 08:11:34 PM »
Thanks Michael,

Sorry for the late response but i was out biking in the afternoon.

I need the list as lists in a list. They form a pattern for distances on a bracket channel (piping and ducting), and i have plenty of them in a  drawing and i need to count the ones who are the same. (Sounds weired ,i know.) So i want to have all the different situations in a dwg as list in a list, so that i can than check all channels later in the code. But i don't want the same list twice, that is why i wanted to check with member.

Bernd


CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: member function with lists in lists
« Reply #5 on: April 22, 2006, 08:49:06 PM »
Forgive this clumsy attempt. Would this work?

Code: [Select]
(car (vl-remove nil (mapcar '(lambda (x)(equal x xfs)) axfs)))
Obviously for the list to match the elements would have to be in the same order.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: member function with lists in lists
« Reply #6 on: April 22, 2006, 10:33:41 PM »
Not sure I understand, but here's another unoptimized stab to achieve what I think you desire --

Code: [Select]
(defun foo ( list1 list2 )
    (append
        (if (null (member list1 list2))
            (list list1)
        )
        list2
    )   
)

Candidate (list1) already exists in list2, don't add it --

Code: [Select]
(foo '(1 2 3) '((1 2 3) (4 5 6)))

>>> ((1 2 3) (4 5 6))

Candidate (list1) does not exist in list2, add it --

Code: [Select]
(foo '(a b c) '((1 2 3) (4 5 6)))

>>> ((A B C) (1 2 3) (4 5 6))
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Amsterdammed

  • Guest
Re: member function with lists in lists
« Reply #7 on: April 23, 2006, 07:26:15 AM »
Cab, Michael,
The problem is , that (back to my first post) altough in my opinion the second element in axfs is the same as xfs, member (or in cab's function equal ) gives me a cold shoulder and tells me that xfs is NOT allready a member in the axfs list. That is what i don't understand.

Thanks,

Bernd

Amsterdammed

  • Guest
Re: member function with lists in lists
« Reply #8 on: April 23, 2006, 07:50:11 AM »
Michael,

I don't want to merge the lists, i want to keep the lists as list but in one bigger list, containing all my list that describe the different situations. So i thought that i could change your code from yesterday
to
Code: [Select]
(defun MergeLists ( lists / _MergeLists )
    (defun _MergeLists ( list1 list2 )
        (append
          (list
            (vl-remove-if
               '(lambda (item) (member item list2))
                list1
            ))
            (list list2)
        )   
    )
    (   (lambda ( result )
            (foreach lst (cdr lists)
                (setq result
                    (if (<= (length lst) (length result))
                        (_MergeLists lst result)
                        (_MergeLists result lst)
                    )
                )   
            )   
        )
        (car (setq lists (reverse lists)))
    )       
)



to keep the list as lists. But as result the test fails

Code: [Select]
(MergeLists
   '(
        (1 2 3)
        (1 2 3)
        (4 5 6)
    )
)



gives

Code: [Select]
(((1 2 3) (4 5 6)) (1 2 3))
Bernd

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: member function with lists in lists
« Reply #9 on: April 23, 2006, 08:01:38 AM »
Hi Bernd. I'm real sorry I'm failing to appreciate what you want. But I won't give up just yet.

Maybe you could provide some more examples of sample data, both preprocessed and processed, reflecting what you want, not what you've tried.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Amsterdammed

  • Guest
Re: member function with lists in lists
« Reply #10 on: April 23, 2006, 08:42:36 AM »
Micheal,
Morning Early Bird

This is what i came up now with
Code: [Select]

(defun List_already_in_list(xfs axfs)
  (foreach x axfs(if (= (length x)(length xfs))
                   (if (foreach n x (equal (nth (vl-position n x)xfs)n ))
                   (princ 't))))

   )

And i use it with if like the member function
Code: [Select]
(if (not (List_already_in_list xfs axfs))
     (setq axfs (append axfs (list xfs)))
   
)

Thanks for pointing me into direction,

Bernd


Amsterdammed

  • Guest
Re: member function with lists in lists
« Reply #11 on: April 23, 2006, 09:41:03 AM »
Well,
That is no good like i wrote it. It only evaluates the last member of my list. So i have to look further from here...

Amsterdammed

  • Guest
Re: member function with lists in lists
« Reply #12 on: April 23, 2006, 10:44:46 AM »
That works. I know, the code looks like i get paid by the line of code i write, but it works.

Code: [Select]
(defun List_not_already_in_list (xfs axfs / flag test)
 (setq flag t)
  (if axfs ;for the first run
    (progn
  (foreach x axfs
    (if (= (length x) (length xfs))
      (progn
      (setq test (mapcar '(lambda (n)
                            (equal (nth (vl-position n xfs) x) n)
                            ) ;_ end of lambda
                         xfs

                         ) ;_ end of if
            )
  (if (not(member 'nil test)) ;they are all the same
        (setq flag nil))

)
      ;_ end of foreach

      ) ;_ end of if
    ) ;_ end of foreach
 

)
    )
   
 
 


(princ flag)
)



So now i can use it as a control function, it give t if it should append the list and nil if not.

Bernd