Author Topic: prelst and sufflst - witch code is the best ?  (Read 5697 times)

0 Members and 1 Guest are viewing this topic.

ribarm

  • Gator
  • Posts: 3265
  • Marko Ribar, architect
prelst and sufflst - witch code is the best ?
« on: October 16, 2012, 02:39:22 PM »
I can't decide witch code is the best for usage :

Code: [Select]
(setq l '(0 1 2 3 4 5 6 7 8 9))
(prelst l 5) => (0 1 2 3 4)
(sufflst l 5) => (6 7 8 9)

Here they are :
- 1st version
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( lst el / q lstn )
  2.   (foreach e lst
  3.     (if (equal e el 1e-8) (setq q T))
  4.     (if (not q) (setq lstn (cons e lstn)))
  5.   )
  6.   (reverse lstn)
  7. )
  8.  
Code - Auto/Visual Lisp: [Select]
  1. (defun sufflst ( lst el / q lstn )
  2.   (foreach e lst
  3.     (if (equal e el 1e-8) (setq q T))
  4.     (if q (setq lstn (cons e lstn)))
  5.   )
  6.   (cdr (reverse lstn))
  7. )
  8.  
- 2nd version
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( lst el / lstbr lstn )
  2.   (defun lstbr ( lst el )
  3.     (if (not (equal (car lst) el 1e-8))
  4.       (progn
  5.         (setq lstn (cons (car lst) lstn))
  6.         (lstbr (cdr lst) el)
  7.       )
  8.     )
  9.   )
  10.   (lstbr lst el)
  11.   (reverse lstn)
  12. )
  13.  
Code - Auto/Visual Lisp: [Select]
  1. (defun sufflst ( lst el / lstbr q lstn )
  2.   (defun lstbr ( lst el )
  3.     (if (car lst)
  4.       (progn
  5.         (cond ((and (not q) (not (equal (car lst) el 1e-8))) (lstbr (cdr lst) el))
  6.               ((and q (not (equal (car lst) el 1e-8))) (setq lstn (cons (car lst) lstn)) (lstbr (cdr lst) el))
  7.               ((equal (car lst) el 1e-8) (setq q T lstn (cons (car lst) lstn)) (lstbr (cdr lst) el))
  8.         )
  9.       )
  10.     )
  11.   )
  12.   (lstbr lst el)
  13.   (cdr (reverse lstn))
  14. )
  15.  
- 3rd version
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( lst el / q k lstn )
  2.   (setq k -1)
  3.   (while (not q)
  4.     (setq lstn (cons (nth (setq k (1+ k)) lst) lstn))
  5.     (if (equal (nth k lst) el 1e-8) (setq q T))
  6.   )
  7.   (reverse (cdr lstn))
  8. )
  9.  
Code - Auto/Visual Lisp: [Select]
  1. (defun sufflst ( lst el / q k lstn )
  2.   (setq k -1)
  3.   (while (not q)
  4.     (setq k (1+ k))
  5.     (if (equal (nth k lst) el 1e-8) (setq q T lstn (cons (nth k lst) lstn)))
  6.   )
  7.   (while (< k (- (length lst) 1))
  8.     (setq k (1+ k))
  9.     (setq lstn (cons (nth k lst) lstn))
  10.   )
  11.   (cdr (reverse lstn))
  12. )
  13.  

Give me some opinion, or maybe 4th version - I don't know...
Thanks, M.R.

P.S. I don't want to use (member '5 l) instead of (sufflst l 5) - here I have fuzz checking 1e-8...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: prelst and sufflst - witch code is the best ?
« Reply #1 on: October 16, 2012, 05:15:59 PM »
vl-member-if

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: prelst and sufflst - witch code is the best ?
« Reply #2 on: October 16, 2012, 05:40:59 PM »
Code - Auto/Visual Lisp: [Select]
  1. (setq l '(0 1 2 3 4 5 6 7 8 9))
  2. (reverse (cdr(member 5 (reverse l)))) => (0 1 2 3 4)
  3. (cdr(member 5 l)) => (6 7 8 9)

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: prelst and sufflst - witch code is the best ?
« Reply #3 on: October 16, 2012, 05:45:55 PM »
Code - Auto/Visual Lisp: [Select]
  1. (setq l '(0 1 2 3 4 5 6 7 8 9))
  2. (reverse (cdr(member 5 (reverse l)))) => (0 1 2 3 4)
  3. (cdr(member 5 l)) => (6 7 8 9)

P.S. I don't want to use (member '5 l) instead of (sufflst l 5) - here I have fuzz checking 1e-8...

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: prelst and sufflst - witch code is the best ?
« Reply #4 on: October 16, 2012, 05:46:10 PM »
Code: [Select]
(defun prelst(l i / n r)
  (while (and (setq n (car l))(not(equal n i 1e-8)))
    (setq r (cons n r) l (cdr l)))
  (reverse r)
)
(defun suflst(l i / n r c)
  (setq l (reverse l)  c (length l))
  (while (and (setq n (car l))(not(equal n i 1e-8)))
    (setq r (cons n r) l (cdr l)))
  (if (/= (length r) c) r)
)
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.

chlh_jd

  • Guest
Re: prelst and sufflst - witch code is the best ?
« Reply #5 on: October 18, 2012, 12:48:29 PM »
If the List is not the same elements
Code: [Select]
(defun sufflst (p l eps)
  (cond ((not l)  nil)
  ((if (numberp eps) (equal p (car l) eps) (equal p (car l))) (cdr l))
  ((sufflst p (cdr l) eps))
  ))
(defun prelst (p l eps)
  (cond ((not l)  nil)
  ((if (numberp eps) (equal p (car l) eps) (equal p (car l)))  nil)
  ((cons (car l) (prelst p (cdr l) eps))
  )))
Code: [Select]
(preflst 5 '(0 1 2 3 4 5 6 7 8 9 10) 1e-8);;-->'(0 1 2 3 4)
(suffflst 5 '(0 1 2 3 4 5 6 7 8 9 10) 1e-8);;-->'(6 7 8 9 10)
« Last Edit: October 18, 2012, 12:56:52 PM by chlh_jd »

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: prelst and sufflst - witch code is the best ?
« Reply #6 on: October 18, 2012, 01:00:59 PM »
vl-member-if
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( x l )
  2.     (reverse (cdr (vl-member-if '(lambda ( a ) (equal a x 1e-8)) (reverse l))))
  3. )
  4. (defun suflst ( x l )
  5.     (cdr (vl-member-if '(lambda ( a ) (equal a x 1e-8)) l))
  6. )

Code - Auto/Visual Lisp: [Select]
  1. _$ (setq l '(0 1 2 3 4 5 6 7 8 9))
  2. (0 1 2 3 4 5 6 7 8 9)
  3. _$ (prelst 5 l)
  4. (0 1 2 3 4)
  5. _$ (suflst 5 l)
  6. (6 7 8 9)

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: prelst and sufflst - witch code is the best ?
« Reply #7 on: October 18, 2012, 01:05:23 PM »
If no duplicates
Code - Auto/Visual Lisp: [Select]
  1. (defun ph:suff (l n)
  2.   (if (equal n (car l) 1e-8) (cdr l) (if l (ph:suff (cdr l) n)))
  3.   )
  4. (defun ph:pref (l n)
  5.   (reverse (ph:suff (reverse l) n))
  6.   )
Code: [Select]
_$ (ph:suff '(0 pi 1.24 a 10 b 11 c r) 10)
(B 11 C R)
_$ (ph:pref '(0 pi 1.24 a 10 b 11 c r) 10)
(0 PI 1.24 A)

chlh_jd

  • Guest
Re: prelst and sufflst - witch code is the best ?
« Reply #8 on: October 19, 2012, 04:03:02 AM »
If the List has same elements , Use reverse will get deffrent result .
e.g.
Code: [Select]
'(0 1 2 3 4 5 5 6 7 8 9 10)
Lee's and Stefan's prelst function will get '(0 1 2 3 4 5) , Mine will get '(0 1 2 3 4) , which is Ribarm wanted , Or the List has not same elements ?

I think , if the function used in  Pointset , it perhaps has same points .

ribarm

  • Gator
  • Posts: 3265
  • Marko Ribar, architect
Re: prelst and sufflst - witch code is the best ?
« Reply #9 on: October 19, 2012, 05:13:45 AM »
Yes, chlh_jd, you're right I was looking for solution with list that may have same elements - like point list thus fuzz 1e-8, and your code is just what I want, but now that I have 4 versions I still can't decide what's the best for usage... Recursion is good but I may encounter hard stack error - or I am wrong - while option is also fast enough, so I implemented CAB's code in his modified by me break object lsp posted on www.cadtutor.net (so that look like it's all CAB's) - fortunately the code concept is that way that it doesn't matter if there are the same points in list so it works... But if I was to ask then my 3rd version with while is near to that what satisfies my needs, and maybe first with foreach if I have to step through all elements in list - seems that this is unavoidable - all list must be processed, because of sufflst in order to imitate real (member or vl-member-if function)... Or maybe combination :

my 3rd version prelst :
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( lst el / q k lstn )
  2.  (setq k -1)
  3.  (while (not q)
  4.    (setq lstn (cons (nth (setq k (1+ k)) lst) lstn))
  5.    (if (equal (nth k lst) el 1e-8) (setq q T))
  6.  )
  7.  (reverse (cdr lstn))
  8. )
  9.  
Lee Mac's version suflst :
Code - Auto/Visual Lisp: [Select]
  1. (defun suflst ( lst el )
  2.    (cdr (vl-member-if '(lambda ( a ) (equal a el 1e-8)) lst))
  3. )
  4.  

M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

chlh_jd

  • Guest
Re: prelst and sufflst - witch code is the best ?
« Reply #10 on: October 19, 2012, 05:28:16 AM »
Though Lee Mac suggest method , you can use 'vl-member-if-not function .
Code: [Select]
(defun prelst (x l / n)
  (setq n (length (vl-member-if-not '(lambda (a) (equal a x 1e-8)) l))
l (reverse l))
  (repeat (/ n 4)
    (setq l (cddddr l)))
  (repeat (rem n 4)
    (setq l (cdr l)))
  (reverse l))

Another question is when not match the give atom , return nil or lst ?
Code: [Select]
(ss:prelst 4.5 '(0 1 2 3 4 5 5 6 7 8 9 10) 1e-8);_-->  '(0 1 2 3 4 5 5 6 7 8 9 10)
(LM:prelst 4.5 '(0 1 2 3 4 5 5 6 7 8 9 10));_--> NIL
which result do you want ?
« Last Edit: October 19, 2012, 05:37:08 AM by chlh_jd »

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: prelst and sufflst - witch code is the best ?
« Reply #11 on: October 19, 2012, 05:36:57 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( x l / f )
  2.     (vl-remove-if '(lambda ( a ) (or f (setq f (equal a x 1e-8)))) l)
  3. )
Code - Auto/Visual Lisp: [Select]
  1. (defun suflst ( x l / f )
  2.     (cdr (vl-remove-if-not '(lambda ( a ) (or f (setq f (equal x a 1e-8)))) l))
  3. )
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( x l )
  2.     (if (and l (not (equal x (car l) 1e-8)))
  3.         (cons (car l) (prelst x (cdr l)))
  4.     )
  5. )
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( x l / r )
  2.     (vl-some '(lambda ( a ) (or (equal x a 1e-8) (not (setq r (cons a r))))) l)
  3.     (reverse r)
  4. )
« Last Edit: October 19, 2012, 06:48:11 AM by Lee Mac »

chlh_jd

  • Guest
Re: prelst and sufflst - witch code is the best ?
« Reply #12 on: October 19, 2012, 05:42:04 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( x l / f )
  2.     (vl-remove-if '(lambda ( a ) (or f (setq f (equal a x 1e-8)))) l)
  3. )
Nice code !  Lee  :-)

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: prelst and sufflst - witch code is the best ?
« Reply #13 on: October 19, 2012, 05:50:11 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun prelst ( x l / f )
  2.     (vl-remove-if '(lambda ( a ) (or f (setq f (equal a x 1e-8)))) l)
  3. )
Nice code !  Lee  :-)

Thank you!  :-)

chlh_jd

  • Guest
Re: prelst and sufflst - witch code is the best ?
« Reply #14 on: October 19, 2012, 07:09:35 AM »
my old version 'eqmember' Compare , it has test by numberlist and pointset . now add Lee Mac's suflst , and just test in my win7 64Bit Acad2011 version VLIDE.
Code: [Select]
(defun f1 (p l eps)
  (cond ((not l)  nil)
  ((if (numberp eps) (equal p (car l) eps) (equal p (car l)))  l)
  ((f1 p (cdr l) eps))
  ))
(defun f2  (a l eps / r b)
  (while(and(not r)l)
    (setq b(car l)l(cdr l))
    (if(if(numberp eps)(equal a b eps)(equal a b))(setq r t)))
  (if r(cons b l)))
(defun f3  (a l eps)
  (member (car (vl-remove-if-not
(function (lambda (x)
     (if (numberp eps)
       (equal a x eps)
       (equal a x))))
l)) l))
(defun Lm:suflst (x l eps) (vl-member-if '(lambda (a) (equal a x eps)) l))
Test function 'Quickbench see here  http://www.theswamp.org/index.php?action=dlattach;topic=42091.0;attach=22832
Code: [Select]
(QuickBench (mapcar '(lambda (f) (list f '5 ''(0 1 2 3 5 4 5 5 6 7 8 9 10) '1e-8)) '(f1 f2 f3 lm:suflst)))
my test result
Code: [Select]

_$
Benchmarking .... done for 8192 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(F1 5 (QUOTE (0 1 2 3 5 4 5 5 6 7 8 ...)      8192      1123      1123      3.11
(F2 5 (QUOTE (0 1 2 3 5 4 5 5 6 7 8 ...)      8192      1155      1155      3.02
(F3 5 (QUOTE (0 1 2 3 5 4 5 5 6 7 8 ...)      8192      1170      1170      2.98
(LM:SUFLST 5 (QUOTE (0 1 2 3 5 4 5 5...)      4096      1746      3492      1.00
--------------------------------------------------------------------------------
_$