I want to write a function that removes trailing nil's from a list. I first tried to write it elegantly with recursion, but ended up like this:
(defun strip-tail (lst)
(let ((last-item-pos (position-if-not #'null lst :from-end t)))
(if last-item-pos
(subseq lst 0 (1+ last-item-pos)))))
; Test cases.
(assert (eq nil (strip-tail nil)))
(assert (eq nil (strip-tail '(nil))))
(assert (equal '(a b) (strip-tail '(a b nil nil))))
(assert (equal '(a nil b) (strip-tail '(a nil b nil))))
(assert (equal '(a b) (strip-tail '(a b))))
It's arguably clear, but I'm not convinced. Is there a more lispy way to do it?