我想找到列表的所有可能的连续分区:
(a b c d) => (((a) (b c d)) ((a b) (c d)) ((a b c) (d)) ((a) (b c) (d)) ((a b c d)) ((a) (b) (c) (d)))
解决这个问题的最简单方法是什么?理想情况下不使用计数器。
编辑:
这是我一直在尝试的一个示例,但它并不完全有效(它应该反过来给出答案,但这没关系):
(define split-list-help
(lambda (l h a)
(begin
(display a)
(if
(null? (cdr l))
(list (cons (cons (car l) a) h))
(let
[(a-nosplit (cons (car l) a))
(h-split (if (null? a)
(cons (list (car l)) h)
(cons (list (car l)) (cons a h))))]
(append (split-list-help (cdr l) h-split '())
(split-list-help (cdr l) h a-nosplit)))))))
(split-list-help '(a b c) '() '())
这个想法是我们逐项遍历列表,在每一步我们可以拆分或不拆分,然后我们分支到两个新的迭代,一个有拆分,一个没有拆分。这会产生接近我想要但不完全的结果。