On my day, instead of (loop for a in l appending (ga)) we wrote (mapcan #'gl) . Which is equivalent (apply #'append (mapcar #'gl)) , more or less:
(defun flatten (l) (if l (if (atom l) (list l) (mapcan
So what does that mean in this case? Imagine that you are calling (flatten (list 1 2 3 4 5)) . Each atom of your list falls into the list - it becomes a list of singleton, for example (1) (2) , etc. Then they are all added together, returning to us ... the original list:
( 1 2 3 4 5 ) ( (1) (2) (3) (4) (5) ) ( 1 2 3 4 5 )
Thus, smoothing the list of atoms is an id operation (in Common LISP, #'identity ). Now imagine that you are compacting a list that has atoms, as well as a list of atoms. Again, each item in the list is converted to flatten , and then they all join together. The list of atoms remains as it is, as we just saw. Atoms are on the list. Thus, the addition will return to us all the atoms that were at two levels in the nested list, are now flattened:
( 11 12 (1 2 3 4) 13 ) ( (11) (12) (1 2 3 4) (13) ) ( 11 12 1 2 3 4 13 )
And so on and so forth, for more levels of nesting.
NIL , because items in lists create a problem. NIL is an empty list, and an empty list does not contain anything, so it should not contribute anything. But NIL also an atom. Therefore, we are making a special case for it, so as not to enclose it in a singleton list - leave it as it is, so when you add it it just disappears.
source share