【发布时间】:2018-02-27 22:31:54
【问题描述】:
下面的代码给了我一个列表的子列表列表。代码仍然可以改进以避免使用append,对吧?
(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))
想法?
编辑:请注意,我们实际上是在谈论子列表,而不是子集。也就是说,(1 2) 是子列表,但 (2 4) 不是子列表。
【问题讨论】:
标签: functional-programming common-lisp