Author Topic: remove similar sub-lists from one list  (Read 1586 times)

0 Members and 1 Guest are viewing this topic.

robertocuba

  • Guest
remove similar sub-lists from one list
« on: June 08, 2009, 11:52:51 AM »
hello:

I'm trying to write a test function which remove similar sub-lists from one liste.

Example: (("m1-1" 10 20) ("m1-2" 50 20) ("m1-3" 45 67) ("m1-1" 10 20) ("m1-2" 2 4) ("m1-1" 20 20))

Must be return:

(("m1-1" 10 20) ("m1-2" 50 20) ("m1-3" 45 67) ("m1-2" 2 4))

Thanks !

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: remove similar sub-lists from one list
« Reply #1 on: June 08, 2009, 11:58:15 AM »
By "similar" do you mean "comparing only the key, or first item in each sub list" and does "remove similar sub-lists" mean "keep the first instance"?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: remove similar sub-lists from one list
« Reply #2 on: June 08, 2009, 12:03:19 PM »
Hi

My 'favorite'

Code: [Select]
(defun remove_doubles (lst)
  (if lst
    (cons (car lst) (remove_doubles (vl-remove (car lst) lst)))
  )
)

EDIT: I read too fast.

If The answer to MP's questions in "yes", this should be:

Code: [Select]
(defun remove_same_car (l)
  (if lst
    (cons (car l)
  (remove_same_car
    (vl-remove-if
      (function (lambda (x) (equal (caar l) (car x))))
      (cdr l)
    )
  )
    )
  )
)

But none of these returns the expected result whith the example list:

(remove_doubles lst) --> (("m1-1" 10 20) ("m1-2" 50 20) ("m1-3" 45 67) ("m1-2" 2 4) ("m1-1" 20 20))
(remove_same_car lst) --> (("m1-1" 10 20) ("m1-2" 50 20) ("m1-3" 45 67))
« Last Edit: June 08, 2009, 12:49:14 PM by gile »
Speaking English as a French Frog

robertocuba

  • Guest
Re: remove similar sub-lists from one list
« Reply #3 on: June 08, 2009, 12:27:25 PM »
Why not function with this:

(defun remove_doubles (lst)
  (if lst
    (cons (car lst) (remove_doubles (vl-remove (car lst) lst)))
  )
  (princ lst)
)

(defun c:a ()
  (setq a (list '(("m1-1" 20 20) ("m1-2" 50 20) ("m1-3" 45 67) ("m1-1" 20 20) ("m1-2" 2 4))))

  (remove_doubles a)
)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: remove similar sub-lists from one list
« Reply #4 on: June 08, 2009, 12:33:38 PM »
It would appear this is the solution but naming it eludes me:

Code: [Select]
(defun foo ( lst / a )

    ;; An abused variant of a function penned/posted by Gile

    (if lst
        (cons
            (setq a (car lst))
            (foo
                (vl-remove-if
                    (function
                        (lambda ( b )
                            (and
                                (eq (car a) (car b))
                                (eq (last a) (last b))
                            )
                        )
                    )
                    (cdr lst)
                )
            )
        )
    )
)

(foo '(("m1-1" 10 20) ("m1-2" 50 20) ("m1-3" 45 67) ("m1-1" 10 20) ("m1-2" 2 4) ("m1-1" 20 20)))

(("m1-1" 10 20) ("m1-2" 50 20) ("m1-3" 45 67) ("m1-2" 2 4))
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

robertocuba

  • Guest
Re: remove similar sub-lists from one list
« Reply #5 on: June 08, 2009, 12:42:59 PM »
Yes !

With the foo function is ok !!!!

Thanks

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: remove similar sub-lists from one list
« Reply #6 on: June 08, 2009, 12:59:09 PM »
A little tighter but not much:

Code: [Select]
(defun foo ( lst )
    (if lst
        (cons (car lst)
            (foo
                (vl-remove-if
                   '(lambda (b)
                        (and
                            (eq (caar lst) (car b))
                            (eq (caddar lst) (caddr b))
                        )
                    )
                    (cdr lst)
                )
            )
        )
    )
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst