写了个 reduce
版的:
(cl-reduce ;; (-reduce
(lambda (acc it) ;; (lambda (acc it)
(if (listp it)
(cons nil (append acc (list it)))
(if (eq nil (car acc))
(append (cdr acc) `((,it)))
(setf (cdr (last (car (last acc)))) `(,it))
acc)))
(cons nil '((1 2 3) 4 (5 6) 7 8)))
;; => ((1 2 3) (4) (5 6) (7 8))
效率好像不太行(-reduce
略优于 cl-reduce
),而且这个写法有时(当输入列表的尾部为 list)输出结果的头部多一个 nil 需要剔除,例如:
<= '((1 2 3) 4 (5 6) 7 8 9 (10))
=> '(nil (1 2 3) (4) (5 6) (7 8 9) (10))