Author Topic: remove elements from a list with identifier of sub-list (recursive)  (Read 11410 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 #30 on: June 09, 2009, 09:24:03 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)))
)

Interesting, similar but different. Like I said a couple posts back, it scares me sometimes that our thought patterns are like those shared by brothers ... Lispin' Bruthas. :D
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://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 #31 on: June 09, 2009, 10:00:16 AM »
Similar but ... Gile's performance is far better:

Elapsed milliseconds / relative speed for 16384 iteration(s):

    (FOO_GILE LST).....1171 / 1.72 <fastest>
    (FOO_MP1 LST)......1891 / 1.07
    (FOO_MP2 LST)......2015 / 1.00 <slowest>


If I replace the calls to type and member with numberp and vl-position respectively:

Elapsed milliseconds / relative speed for 16384 iteration(s):

    (FOO_GILE LST).....1171 / 1.29 <fastest>
    (FOO_MP1 LST)......1344 / 1.13
    (FOO_MP2 LST)......1516 / 1.00 <slowest>


Gile's is still the better algorithm; kudos. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://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 #32 on: June 09, 2009, 10:23:38 AM »
However!

(setq lst '("a" 3 4 5 5 "b" 8 7 6 3 2 2 1 1 "c" 5 67 87 76 "d" 9 0 5 49 5 9))

(foo_gile lst)

=> ("a" 3 4 5 "b" 8 7 6 3 2 1 "c" 5 67 87 76)

(foo_mp1 lst)

=> ("a" 3 4 5 "b" 8 7 6 3 2 1 "c" 5 67 87 76 "d" 9 0 5 49)

(foo_mp2 lst)

=> ("a" 3 4 5 "b" 8 7 6 3 2 1 "c" 5 67 87 76 "d" 9 0 5 49)

Hello:

Iīm developing a following routine to remove elements from a list between identifiers "x" .... "x" of sub-list (recursive).

Problem:
List: ("a" 3 4 5 5 "b" 8 7 6 3 2 2 1 1 "c" 5 67 87 76 "d" 9 0 5 49 5 9)

The routine must be return:

List:  ("a" 3 4 5 "b" 8 7 6 3 2 1 "c" 5 67 87 76 "d" 9 0 5 49)

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

uncoolperson

  • Guest
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #33 on: June 09, 2009, 10:57:28 AM »
wow, mine is really slow in comparison

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 #34 on: June 09, 2009, 11:03:15 AM »
wow, mine is really slow in comparison

Whao, I thought you were being sarcastic:

Elapsed milliseconds / relative speed for 16384 iteration(s):

    (FOO_MP1 LST)......1375 / 17.74 <fastest>
    (FOO_MP2 LST)......1563 / 15.61
    (FOO_UCP LST).....24391 / 1.00 <slowest>


The good news is ... (foo_ucp lst)

=> ("a" 3 4 5 "b" 8 7 6 3 2 1 "c" 5 67 87 76 "d" 9 0 5 49)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #35 on: June 09, 2009, 11:09:55 AM »
Ooopss !!!
I didn't saw that yesterday, it was too late here
Here's a corrected one, I expect...  one more line (if we count the lines :wink:)

Code: [Select]
(defun gile (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)))
      )
      (reverse tmp)
    )
  )
  (sub (cdr lst) (list (car lst)))
)
Speaking English as a French Frog

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 #36 on: June 09, 2009, 11:13:55 AM »
Ooopss !!!
I didn't saw that yesterday, it was too late here
Here's a corrected one ...

Well done my friend:

Elapsed milliseconds / relative speed for 16384 iteration(s):

    (FOO_GILE LST)......1203 / 20.22 <fastest>
    (FOO_MP1 LST).......1391 / 17.49
    (FOO_MP2 LST).......1515 / 16.06
    (FOO_UCP LST)......24328 / 1.00 <slowest>


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

uncoolperson

  • Guest
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #37 on: June 09, 2009, 11:18:15 AM »
wow, mine is really slow in comparison

Whao, I thought you were being sarcastic:

Elapsed milliseconds / relative speed for 16384 iteration(s):

    (FOO_MP1 LST)......1375 / 17.74 <fastest>
    (FOO_MP2 LST)......1563 / 15.61
    (FOO_UCP LST).....24391 / 1.00 <slowest>


The good news is ... (foo_ucp lst)

=> ("a" 3 4 5 "b" 8 7 6 3 2 1 "c" 5 67 87 76 "d" 9 0 5 49)

probably the length and member-if

JohnK

  • Administrator
  • Seagull
  • Posts: 10634
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #38 on: June 09, 2009, 01:05:17 PM »
This thread got busy.

I built a tail recursive (recursive procedure using an iterative process) procedure this morning. Its not very fast nor has it been ``cleaned'' --in fact, its fairly messy and almost incoherent-. If you want, you can time it but it will most likely be slow compared to the others in this thread (I didnt look at the code in this thread, but kinda felt i knew the direction the posters of this thread would go in so I did it to be different not fast).


Code: [Select]
(defun remove_dups-iter (tmp sofar lis)
  (if (null lis)
    ;; cleanup
    (reverse (apply 'append (cons tmp sofar)))
   
    (remove_dups-iter
      ;; re iterate
        (cons
          ;; tmp list
          (if (not (member (car lis) tmp)) (car lis) )
          (if (eq (type (car lis)) 'STR) 'nil tmp) )
               
        (cond
          ;; sofar list
          ((eq (type (car lis)) 'STR)
               (setq sofar (cons tmp sofar))
               (setq tmp nil)
               sofar)
          ( sofar ) )
       
        ;; remainder of list
        (cdr lis))) )

(defun remove_dups_in-between-chars ( lst / tmp )
  ;; a cleaner interface
   (remove_dups-iter '() '() lst) )

(remove_dups_in-between-chars '("a" 3 4 5 5 "b" 8 7 6 3 2 2 1 1 "c" 5 67 87 76 "d" 9 0 5 49 5 9))

=> ("a" 3 4 5 nil "b" 8 7 6 3 2 nil 1 nil "c" 5 67 87 76 "d" 9 0 5 49 nil nil)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #39 on: June 09, 2009, 05:05:28 PM »
Late entry, non recursive. 8-)
Code: [Select]
(defun NoDoups (lst / e nlst tmp)
  (while (setq e (car lst))
    (setq lst (cdr lst))
    (if (numberp e)
      (if (or (null tmp) (not (vl-position e tmp)))
        (setq tmp (cons e tmp))
      )
      (progn
        (if tmp (setq tmp (cons e tmp)))
        (if nlst
          (setq nlst (append tmp nlst))
          (setq nlst (cons e tmp))
        )
        (setq tmp nil)
      )
    )
  )
  (if tmp (setq nlst (append tmp nlst)))
  (reverse nlst)
)
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.

uncoolperson

  • Guest
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #40 on: June 09, 2009, 05:37:04 PM »
Code: [Select]
(defun doit2 (thelist)
  (reverse (remove2 (reverse thelist)))
)
(defun remove2 (alist)
  (if alist
    (if
      (if (numberp (setq thelist alist
search (car alist)))
(progn
  (While (and (/= (setq thelist (cdr thelist)
testitem (car thelist)
  )
  search
      )
      (numberp testitem)
)
  )
  (numberp testitem)
)
      )
       (remove2 (cdr alist))
       (cons search (remove2 (cdr alist)))
    )
  )
)



Quote
Benchmarking ................Elapsed milliseconds / relative speed for 8192 iteration(s):

    (NODOUPS BLAH)...........................1031 / 22.51 <fastest>
    (GILEFOO BLAH)...........................1125 / 20.62
    (DOIT2 BLAH).............................1484 / 15.64
    (GROUPSET BLAH)..........................1750 / 13.26
    (MP2FOO BLAH)............................1829 / 12.69
    (MP1FOO BLAH)............................1844 / 12.58
    (MP3FOO BLAH)............................1875 / 12.37
    (REMOVE_DUPS_IN-BETWEEN-CHARS BLAH)......2484 / 9.34
    (PROCESS_LIST BLAH)......................2890 / 8.03
    (DOIT BLAH).............................23203 / 1.00 <slowest>

catchin up

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #41 on: June 09, 2009, 05:43:38 PM »
Oh, I like that test run.  :-)
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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10634
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #42 on: June 09, 2009, 05:50:48 PM »
well were you expecting anything else? ...try using foreach instead and you will really smoke us.

MP, quick question; I never quite understood what "relative speed for ... iteration(s):" meant? Is that the time per call?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2135
  • class keyThumper<T>:ILazy<T>
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #43 on: June 09, 2009, 05:50:58 PM »
Alan's non-recursive plain-jane version scoops the money, yes ?
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

JohnK

  • Administrator
  • Seagull
  • Posts: 10634
Re: remove elements from a list with identifier of sub-list (recursive)
« Reply #44 on: June 09, 2009, 05:52:16 PM »
yeah and he did it with a while loop too!?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org