I am currently trying to remove any NIL I find from a list (recursively) on all levels. I already know how to remove NIL from the top level of a list, and I thought much of the idea would be the same when dealing with multiple levels, however, I have run into a snag.
My code for removing Nil from the top level:
(defun removeNILTop (L)
(cond ( (NULL L) NIL) ;;list is empty
( (NULL (CAR L)) (removeNILTop( CDR L))) ;;Nil so skip it
( T (CONS( CAR L) (removeNILTop( CDR L)))) ;;not NIL so include it
)
)
This my code for removing Nil from all levels:
(defun removeAll (l)
(cond
((null l) NIL) ;;empty list
((null (car l)) (removeAll(cdr l))) ;;Nil so skip it
((atom (car l)) (cons (car l) (removeAll(cdr l)))) ;;not nil and is a atom so continue normally
(T (cons( removeAll(car l) (removeAll(cdr l))))) ;;car is a list recurse into it
)
)
The way I was thinking about this is that in the first example I ignore what the car of the list and just keep it as long as it's not null. However, now that I care about it, I should check if the car is
- an atom and not nil, if it is then I can proceed normally
- otherwise, if it is a list then I should recurse into it, and cons the result to the result of the cdr.
This obviously is however not working, any tips?