What I'm looking for
I want to have a function that given the following inputs, returns the following outputs.
Input
(1 nil)
(1 (2) (nil))
(1 (2 (3)) (nil (nil)))
(1 (2 (3 (4))) (nil (nil (nil))))
(1 (2 (3 (4 (5)))) (nil (nil (nil (nil)))))
Output
(1 nil)
(1 2 nil)
(1 2 3 nil nil)
(1 2 3 4 nil nil nil)
(1 2 3 4 5 nil nil nil nil)
Background
Some days ago, I asked a question on the same topic, but I hadn't explicitly stated that I wanted to keep nil
values, so I'm creating a new question.
This answer to a question that was linked to my question recommended using flatten-tree
. This comment recommended using -flatten
from the dash.el
package. I have used both, but none of them keep nil
values. See output of evaluation below.
(let ((cases '((1 nil)
(1 (2) (nil))
(1 (2 (3)) (nil (nil)))
(1 (2 (3 (4))) (nil (nil (nil))))
(1 (2 (3 (4 (5)))) (nil (nil (nil (nil))))))))
(cl-loop
for case in cases
do (progn
(princ (format "flatten-tree: %s\n" (flatten-tree case)))
(princ (format "-flatten: %s\n" (-flatten case))))))
flatten-tree: (1)
-flatten: (1)
flatten-tree: (1 2)
-flatten: (1 2)
flatten-tree: (1 2 3)
-flatten: (1 2 3)
flatten-tree: (1 2 3 4)
-flatten: (1 2 3 4)
flatten-tree: (1 2 3 4 5)
-flatten: (1 2 3 4 5)
C-h f -flatten
, although I suspect neither suggestion will be palatable.nil
as "meaningful" elements in a list is asking for trouble. It will require constant vigilance to make sure that everything you do maintains the structure and after a while it will be exhausting. I would recommend that you change the data representation. But I can't really point to a fire: I just think that I smell smoke.gap
orempty
) instead ofnil
to avoid such problems.nil
and empty list.