2

我想找到列表的所有可能的连续分区:

(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) '() '())

这个想法是我们逐项遍历列表,在每一步我们可以拆分或不拆分,然后我们分支到两个新的迭代,一个有拆分,一个没有拆分。这会产生接近我想要但不完全的结果。

4

2 回答 2

3

目标是找到一种使用递归来描述问题的自然方式。为了找到子列表,(a b c d)我们可以关注元素a。有四个不同的连续子列表,其中包含a

(a)  (a b)  (a b c)  (a b c d)

在每种情况下,我们都需要找到剩余元素的子列表。总而言之,结果必须是来自的列表集合

combining (a)       with (sublists '(b c d))
combining (a b)     with (sublists   '(c d))
combining (a b c)   with (sublists     '(d))
combining (a b c d) with (sublists     ' ())

那就是我们有:

(sublists '(a b c d)) = (append (combine '(a)       (sublists '(b c d)))
                                (combine '(a b)     (sublists   '(c d)))
                                (combine '(a b c)   (sublists    '(d)))
                                (combine '(a b c d) (sublists     '())))

我们注意到我们已经使用只有三个元素的子列表的递归调用来描述列表四个元素的子列表。基本情况(sublists '())必须返回空列表'()

剩下的唯一问题是 combine 是做什么的。让我们检查一下案例中输入和输出之间的关系

(combine '(a) (sublists '(b c d)))

的子列表'(b c d)是:

( ((b) (c) (d))
  ((b) (c d)  )
  ((b c) (d)  )
  ((b c d)    ) )

所以(combine '(a) (sublists '(b c d)))必须返回

( ((a) (b) (c) (d))
  ((a) (b) (c d)  )
  ((a) (b c) (d)  )
  ((a) (b c d)    ) )

在列表前面预置一个元素(列表'(a))的操作是 cons,所以我们可以一起使用mapcons

(define (combine x xss)
  (map (lambda (xs) (cons x xs)) ; function that prepends x to a list xs
       xss))

现在我们有了所有的拼图。我将把子列表的最终定义留给你。

于 2018-06-24T17:48:52.710 回答
1

既然你提到了 miniKanren,这里是这个问题的 Prolog 解决方案:

splits(L, LS):-                % conde ...
  (   L  = []                  % L is empty list:
  ->  LS = []
  ;                            % OR
      A = [_ | _],             % A is non-empty,
      append(A, B, L),         % for each A, B such that A + B = L,
      splits(   B, BS),        %   for every splits BS of B,   
      LS = [ A |   BS]         %     prepend A to BS to get the splits of L
  ).

%%% in SWI Prolog:
?- splits([1,2,3,4], R).
R = [[1], [2], [3], [4]] ;
R = [[1], [2], [3, 4]] ;
R = [[1], [2, 3], [4]] ;
R = [[1], [2, 3, 4]] ;
R = [[1, 2], [3], [4]] ;
R = [[1, 2], [3, 4]] ;
R = [[1, 2, 3], [4]] ;
R = [[1, 2, 3, 4]] ;
false.

翻译成 miniKanren 这将定义splitso为一个condewith anappendo和一个递归调用splitso

#lang racket
(require minikanren)

(define (splitso L LS)
  (conde
   [(== L '()) (== LS '())]
   [(fresh (A B BS _H _T)
           (== A `(,_H . ,_T))
           (appendo A B L)
           (== LS `(,A . ,BS))    
           (splitso   B   BS))]))    

;;;
> (run* (R) (splitso '(1 2 3 4) R))
'(((1 2 3 4))
  ((1) (2 3 4))
  ((1 2) (3 4))
  ((1) (2) (3 4))
  ((1 2 3) (4))
  ((1) (2 3) (4))
  ((1 2) (3) (4))
  ((1) (2) (3) (4)))

appendo我从这里复制。

miniKanren 中的解决方案顺序不遵循谓词定义中的目标顺序(就像在 Prolog 中那样),因为 miniKanren 将子目标产生的结果交错以实现所谓的“公平调度”。

于 2018-06-25T10:08:22.783 回答