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\$
2
  • \$\begingroup\$ It's OK for the output to appear in any order, right? \$\endgroup\$ Commented Mar 1, 2018 at 1:18
  • \$\begingroup\$ yes, it is fine. \$\endgroup\$ Commented Apr 6, 2018 at 3:35

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.