Author Topic: Remove NIL values in a LIST  (Read 3616 times)

0 Members and 1 Guest are viewing this topic.

mailmaverick

  • Bull Frog
  • Posts: 493
Remove NIL values in a LIST
« on: September 05, 2016, 09:51:38 AM »
How to remove NIL values in a LIST ?

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

kpblc

  • Bull Frog
  • Posts: 396
Re: Remove NIL values in a LIST
« Reply #2 on: September 05, 2016, 10:36:22 AM »
(vl-remove nil lst)
:)
Sorry for my English.

mailmaverick

  • Bull Frog
  • Posts: 493
Re: Remove NIL values in a LIST
« Reply #3 on: September 05, 2016, 11:04:25 AM »
Thanks a lot.

Sometimes we are so dumb :?

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Remove NIL values in a LIST
« Reply #4 on: September 05, 2016, 11:25:06 AM »
Sometimes we are so dumb :?

(repeat 2 <game buzzer>)

Sorry, that is incorrect.



Not asking for help?

Now that would be dumb.

PS: The people you may consider gurus have asked LOTS of questions themselves.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Remove NIL values in a LIST
« Reply #5 on: September 05, 2016, 12:27:23 PM »
Some variations/benchmarks for academia:
Code - Auto/Visual Lisp: [Select]
  1. (defun remove-nil-1 ( lst ) (vl-remove nil lst))
  2. (defun remove-nil-2 ( lst ) (vl-remove-if 'null lst))
  3. (defun remove-nil-3 ( lst ) (apply 'append (subst nil '(nil) (mapcar 'list lst))))
  4. (defun remove-nil-4 ( lst ) (apply 'append (mapcar '(lambda ( x ) (if x (list x))) lst)))
  5. (defun remove-nil-5 ( lst / rtn ) (foreach x lst (if x (setq rtn (cons x rtn)))) (reverse rtn))
  6. (defun remove-nil-6 ( lst ) (if lst (if (car lst) (cons (car lst) (remove-nil-6 (cdr lst))) (remove-nil-6 (cdr lst)))))
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq lst '(1 2 nil 3 4 nil 5 6 nil))
  2. (1 2 nil 3 4 nil 5 6 nil)
  3. _$ (repeat 4 (setq lst (append lst lst)))
  4. (1 2 nil 3 4 nil 5 6 nil 1 2 nil ... nil 5 6 nil)
  5. _$ (length lst)
  6. 144
  7.  
  8. _$ (benchmark '((remove-nil-1 lst)(remove-nil-2 lst)(remove-nil-3 lst)(remove-nil-4 lst)(remove-nil-5 lst)(remove-nil-6 lst)))
  9. Benchmarking ....................Elapsed milliseconds / relative speed for 131072 iteration(s):
  10.  
  11.     (REMOVE-NIL-1 LST).....1669 / 5.16 <fastest>
  12.     (REMOVE-NIL-2 LST).....2792 / 3.08
  13.     (REMOVE-NIL-4 LST).....6723 / 1.28
  14.     (REMOVE-NIL-3 LST).....6724 / 1.28
  15.     (REMOVE-NIL-5 LST).....7379 / 1.17
  16.     (REMOVE-NIL-6 LST).....8611 / 1.00 <slowest>

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Remove NIL values in a LIST
« Reply #6 on: September 05, 2016, 01:48:34 PM »
This is Lee Mac right there, a.k.a. the List Manipulator. Thanks for posting this!
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg