所有可能的子列表方案

时间:2018-06-24 16:10:12

标签: scheme minikanren

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

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

这个想法是我们逐项遍历列表,在每一步我们都可以拆分它或不拆分它,然后我们分成两个新的迭代,一个是拆分的,另一个是不拆分的。这样产生的结果接近我想要的结果,但不完全相同。

2 个答案:

答案 0 :(得分: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 '(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))放在前面的操作 列表中的内容是不利的,因此我们可以同时使用mapcons

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

现在,我们已经解开了所有难题。我将保留最终定义 子列表。

答案 1 :(得分: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定义为具有conde和对appendo的递归调用的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)))

我从here复制了appendo

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

相关问题