Author Topic: remove elements from a list with identifier of sub-list (recursive)  (Read 11414 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #15 on: June 08, 2009, 06:17:57 PM »
You post your attempts first robertocuba . :evil:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

uncoolperson

  • Guest
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #16 on: June 08, 2009, 06:27:50 PM »
single recursive function containing two if statements, one of those empty list check, the other one an "(and".

requires reversal of list prior to use

heck yeah.



robertocuba, hurry up and post some stuff, I wanna show this off.

robertocuba

  • Guest
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #17 on: June 08, 2009, 06:32:53 PM »
This is my first code:

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

(defun process_list (list_p / contador centinel element lista_tmp list_final)
  (setq contador 0)
  (while (< contador (length list_p))
    (progn
      (setq centinel 0)
      (setq element (nth contador list_p))
      (if (= nil (numberp element))
        (progn
     (setq lista_tmp (cons (nth contador list_p) lista_tmp))
     (while (= centinel 0)
       (if (= T (numberp (nth (+ contador 1) list_p)))
         (setq lista_tmp (cons (nth (+ contador 1) list_p) lista_tmp))
         ;else
         (progn
      (setq list_final (cons (remove_doubles lista_tmp) list_final))
           (setq centinel 1)
         )
       )
       (setq contador (+ contador 1))
     )
        )
      )
    )
  )
)


(defun c:x ()
    (setq l (process_list '("m1-1" 1 2 "m1-2" 3 3 4 "m1-3" 1 1 1 2 2 3)))
)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #18 on: June 08, 2009, 06:33:14 PM »
Not pretty but it works and is faster than my other attempts ...

Code: [Select]
(defun foo ( lst / bar )
    (defun bar ( a b / c e )
        (if a
            (bar
                (cdr a)
                (if (eq 'str (type (setq e (car a))))
                    (cons (list e) b)
                    (if (member e (setq c (car b))) b (cons (cons e c) (cdr b)))
                )
            )    
            (apply 'append (reverse (mapcar 'reverse b)))
        )      
    )
    (bar lst nil)
)
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 elements from a list with identifier of sub-list (recursive)
« Reply #19 on: June 08, 2009, 06:35:51 PM »
Excellent !

Thanks

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #20 on: June 08, 2009, 06:36:51 PM »
Don't get too excited, mine's likely the least elegant of them all. Stay tuned ... :)
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: remove elements from a list with identifier of sub-list (recursive)
« Reply #21 on: June 08, 2009, 06:41:48 PM »
Here's one of my other stabs:

Code: [Select]
(defun foo ( lst / bar )
    (defun bar ( a b c / e f )
        (if a
            (bar
                (cdr a)
                (if (setq f (eq 'str (type (setq e (car a)))))
                    (list e)
                    (if (member e b) b (cons e b))
                )
                (if f (cons b c) c)               
            )   
            (apply 'append (reverse (mapcar 'reverse (cons b c))))
        )
    )
    (bar lst nil nil)   
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

uncoolperson

  • Guest
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #22 on: June 08, 2009, 06:53:07 PM »
Code: [Select]
(defun doit (thelist)
  (reverse (remove (reverse thelist)))
)

(defun remove (alist)
  (if alist
    (if
      (and
                (/= (type (car alist)) 'STR)
                (> (length (member (car alist) (cdr alist)))
                   (length
                     (vl-member-if '(lambda (item) (= (type item) 'STR)) alist)
                   )
                )
      )
       (remove (cdr alist))
       (cons (car alist) (remove (cdr alist)))
    )
  )
)

edit: forgot to add, there was some mild editing done without autocad around to check... so might need some minor tweaking to run. I'll check tomorrow.
« Last Edit: June 09, 2009, 12:36:15 AM by uncoolperson »

uncoolperson

  • Guest
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #23 on: June 08, 2009, 07:14:13 PM »
(defun foo ( lst / bar )
    (defun bar ( a b / c e )
        (if a
            (bar
                (cdr a)
               (if (eq 'str (type (setq e (car a))))
                    (cons (list e) b)
                    (if (member e (setq c (car b))) b (cons (cons e c) (cdr b)))
                )

            )    
            (apply 'append (reverse (mapcar 'reverse b)))
        )      
    )
    (bar lst nil)
)


I think my head just exploded with that

in the cool sorta way.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #24 on: June 08, 2009, 07:35:08 PM »
I think my head just exploded with that

in the cool sorta way.

Ditto  :ugly:

Or as you say here "this".

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #25 on: June 08, 2009, 09:44:10 PM »
I think my head just exploded with that

in the cool sorta way.

Glad I could help. :D

Wait 'til you see Gile's.
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: remove elements from a list with identifier of sub-list (recursive)
« Reply #26 on: June 08, 2009, 11:16:12 PM »
Slight variation for fun ...

Code: [Select]
(defun foo ( lst / bar )
    (defun bar ( a b / c e )
        (if (setq c (car b) e (car a))
            (bar
                (cdr a)
                (if (eq 'str (type e))
                    (cons (list e) b)
                    (if (member e c) b (cons (cons e c) (cdr b)))
                )
            )   
            (apply 'append (reverse (mapcar 'reverse b)))
        )       
    )
    (bar lst nil)
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

FengK

  • Guest
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #27 on: June 08, 2009, 11:34:12 PM »
plain. it is easier for me to follow than anything with recursion.

Code: [Select]
(defun GroupSet (lst / r subset)
  (foreach e lst
    (if (eq (type e) 'str)
      (setq r (cons e r)
      subset nil
      )
      (if (not (member e subset))
(setq r (cons e r)
        subset (cons e subset)
)
      )
    )
  )
  (reverse r)
)

uncoolperson

  • Guest
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #28 on: June 09, 2009, 12:35:27 AM »
You never said if this was for a class or not, if it is may I suggest doing them on your own then posting the problems? As you can tell we all love a good challenge, but if it is for a class that's probably something you should work out most on your own.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #29 on: June 09, 2009, 01:34:00 AM »
 :xTime difference ! I was sleeping while you were posting

mine look like MP's

Code: [Select]
(defun foo (lst / sub)
  (defun sub (lst tmp / a)
    (if lst
      (if (numberp (setq a (car lst)))
(if (vl-position a tmp)
  (sub (cdr lst) tmp)
  (sub (cdr lst) (cons a tmp))
)
(append (reverse tmp) (sub (cdr lst) (list a)))
      )
    )
  )
  (sub (cdr lst) (list (car lst)))
)
Speaking English as a French Frog