4
\$\begingroup\$

The code below gives me the list of sublists of a list. Code can still be improved to avoid the use of append, right?

(defun sublists (alist)
  (labels ((aux (list p r)
             (if (null list)
                 (append r (maplist #'identity p))
                 (aux (cdr list)
                      (append p (list (car list)))
                      (append r (maplist #'identity p))))))
    (aux alist nil nil)))


CL-USER> (sublists (list 1 2 3 4))
((1) (1 2) (1 2 3) (1 2 3 4) (2) (2 3) (2 3 4) (3) (3 4) (4))

ideas? This question was posted in https://stackoverflow.com/questions/49019045/pure-functional-code-for-the-sublists-of-a-list originally.

\$\endgroup\$
  • \$\begingroup\$ It's OK for the output to appear in any order, right? \$\endgroup\$ – 200_success Mar 1 '18 at 1:18
  • \$\begingroup\$ yes, it is fine. \$\endgroup\$ – Alexandre Rademaker Apr 6 '18 at 3:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.