Author Topic: How to delete "" nil in the list (1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8  (Read 14385 times)

0 Members and 1 Guest are viewing this topic.

SomeCallMeDave

  • Guest
....
When the list is:
 (list 1245 "" nil "add" "" (list 1 "" 2 nil "" 3 5 "" nil 6 "" 8 9 (list 888 888 "" nil 9999 9999 nil "") 555 "" nil 666) 777 "" nil 77777)
.....

Scope creep   :-D

Back to my little drawing board

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Code: [Select]
(defun killer-n (x a)
    (if (atom x)
      (if (not (eq a x)) x)
      (vl-remove-if 'null
(cons (killer-n (car x) a)
            (killer-n (cdr x) a)))))

Thanks for 'getting w/the program', :lol:. Unfortunately it crashes when using the original poster's data. :(
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.
my variant:
Code: [Select]
(defun f (l)
 (cond ((null l) nil)
       ((atom l) (list l))
       ((listp l) (append (f (car l)) (f (cdr l))))
 ) ;_  cond
) ;_  defun

Very pretty Evgeniy :), but flattens the list instead of merely removing the nils :oops:.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
I have not truly understood a problem, I am corrected...
Code: [Select]
(defun f (l)
 (if l
  (if (listp l)
   (if (car l)
    (cons (f (car l)) (f (cdr l)))
    (f (cdr l))
   ) ;_  if
   l
  ) ;_  if
 ) ;_  if
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
variant 2:
Code: [Select]
(defun f (l)
 (cond ((not l) nil)
       ((atom l) l)
       ((and (listp l) (car l)) (cons (f (car l)) (f (cdr l))))
       ((f (cdr l)))
 ) ;_  cond
)

JohnK

  • Administrator
  • Seagull
  • Posts: 10646

Thanks for 'getting w/the program', :lol:. Unfortunately it crashes when using the original poster's data. :(

*Ack* i didnt see that dot. I'll get back to it when i put out a fire or two.

Your right, nice puzzle.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Very nicely done Evgeniy: both variants 1 & 2.
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.
variant 2:
Code: [Select]
(defun f (l)
 (cond ((not l) nil)
       ((atom l) l)
       ((and (listp l) (car l)) (cons (f (car l)) (f (cdr l))))
       ((f (cdr l)))
 ) ;_  cond
)

Can be reduced to:

Code: [Select]
(defun f (l)
    (cond
        ((null l) nil)
        ((atom l) l)
        ((car l) (cons (f (car l)) (f (cdr l))))
        ((f (cdr l)))
    )
)

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

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Can be reduced to:

Code: [Select]
(defun f (l)
    (cond
        ((null l) nil)
        ((atom l) l)
        ((car l) (cons (f (car l)) (f (cdr l))))
        ((f (cdr l)))
    )
)

Very nicely reduced.
Excellent work!

:)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Not only does the OP want to remove nil but also "" 8-)
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.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Not only does the OP want to remove nil but also "" 8-)

lol, missed that salient point.

Evgeny's would be easy to modify to suit, but revising my own submission:

Code: [Select]
(defun deflate ( x / a )
    (cond
        ((atom x) x)
        ((null (vl-consp x)) (if (vl-every 'atom x)(vl-remove-if 'null x)(mapcar 'deflate x)))
        ((and (setq a (car x))(/= a "")) (cons (deflate a) (deflate (cdr x))))
        ((deflate (cdr x)))
    )
)
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.
Here's a more generic function using Evgeny's submission as a starting point:

Code: [Select]
(defun remove_all ( x lst / a )
    (cond
        ((member x lst) nil)
        ((atom x) x)
        ((member (setq a (car x)) lst) (remove_all (cdr x) lst))
        ((cons (remove_all a lst) (remove_all (cdr x) lst)))
    )
)

(setq lst '(1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8 . 9)))

(remove_all lst '(nil ""))

=> (1245 "add" (1 2 3 5 6 8 . 9))

I reserve the right to reverse the order of the arguments; at present doesn't feel right, but gotta get back to work.
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.
Now named properly, and arguments in proper order (in my mind anyway): :ugly:

Code: [Select]
(defun remove_items ( items lst / a )
    (cond
        ((member lst items) nil)
        ((atom lst) lst)
        ((member (setq a (car lst)) items) (remove_items items (cdr lst)))
        ((cons (remove_items items a) (remove_items items (cdr lst))))
    )
)

(setq lst '(1245 "" "add" (1 "" 2 nil "" 3 5 "" nil 6 "" 8 . 9)))

(remove_items '(nil "") lst)

=> (1245 "add" (1 2 3 5 6 8 . 9))
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

gile

  • Gator
  • Posts: 2507
  • Marseille, France
More generic, using a predicate function

Code: [Select]
(defun nest-remove-if (fun lst / a )
    (cond
        ((apply fun (list lst)) nil)
        ((atom lst) lst)
        ((apply fun (list (setq a (car lst)))) (nest-remove-if fun (cdr lst)))
        ((cons (nest-remove-if fun a) (nest-remove-if fun (cdr lst))))
    )
)

_$ (setq lst  (list 1245 "" nil "add" "" '(1 "" 2 nil "" 3 5 "" nil 6 "" 8 . 9)))
(1245 "" nil "add" "" (1 "" 2 nil "" 3 5 "" nil 6 "" 8 . 9))
_$ (nest-remove-if '(lambda (x) (member x '("" nil))) lst)
(1245 "add" (1 2 3 5 6 8 . 9))
_$ (nest-remove-if 'numberp lst)
("" nil "add" "" ("" nil "" "" nil ""))



Speaking English as a French Frog

Spike Wilbury

  • Guest
after seeing the last functions posted.... I start to remember the x-arch or reni urban site days.... they look to close (and I am not saying that were copied eh!) :)