実際に行っているのは、サイズxのセットのべき集合を構築することです。
べき集合は、可能なすべてのサブセットのセットです。たとえば、(list 1 2 3)のべき集合は(list(list 1 2 3)(list 1 2)(list 1 3)(list 1)(list 2 3)(list 2)(list 3)empty)です。 。
(セットはそれ自体のサブセットであり、空のセットはすべてのセットのサブセットです。)
あなたがしていることがパワーセットを説明している理由は、要素がサブセットに含まれる場合と含まれない場合があるためです。したがって、apply(list true true true)to(list 1 2 3)は(list 1 2 3)を返し、(list false true true)は(list 2 3)を返します。
これはあなたの問題のための私のコードです。
(define baselist (list (list true) (list false)))
;; List1 List2 -> List of Lists
;; Where List1 is any list of lists, and list2 is a list of lists of size 2
;; and all of the lists within list 2 has one element
(define (list-combination list-n list-two)
(cond [(empty? list-n) empty]
[else (cons (append (first list-n) (first list-two))
(cons (append (first list-n) (second list-two))
(list-combination (rest list-n) list-two)))]))
;; tflist Number -> List of Boolean Lists
;; creates baselistn
(define (tflist n)
(cond [(= 1 n) baselist]
[else (list-combination (tlist (sub1 n)) baselist)]))
したがって、(tflist 3)は元の問題を返します。パワーセットを作成するには、次のようにします...
;; subset List1 ListofBooleans -> List
;; Determines which elements of a set to create a subset of
;; List1 and LoB are of the same length
(define (subset set t-f-list)
(cond [(empty? t-f-list) empty]
[(first t-f-list) (cons (first set) (subset (rest set) (rest t-f-list)))]
[else (subset (rest set) (rest t-f-list))]))
;;powerset set -> Powerset
;; produces a powerset of a set
(define (powerset set)
(local ((define upperbound (expt 2 (length set)))
(define tflist (tlist (length set)))
(define (powerset set n)
(cond [(= n upperbound) empty]
[else (cons (subset set (list-ref tflist n)) (powerset set (add1 n)))])))
(powerset set 0)))