嗨,我试图定义一个函数,应该从该集合的部分进行设置。 对于所有属于P(A- {X})的B,应该定义为:P(A)= P(A- {x})U {{x} UB}其中X属于A.计划集从部分集合制成
一种测试将是:
(份“(ABC)) =>((ABC)(AB)(AC)(一)(BC)(b)(C)())
我一直在试图用这一个:
(定义(MAPC FXL) (如果(空L) 升 (cons(f x(car l))(mapc f x(cdr l)))))
嗨,我试图定义一个函数,应该从该集合的部分进行设置。 对于所有属于P(A- {X})的B,应该定义为:P(A)= P(A- {x})U {{x} UB}其中X属于A.计划集从部分集合制成
一种测试将是:
(份“(ABC)) =>((ABC)(AB)(AC)(一)(BC)(b)(C)())
我一直在试图用这一个:
(定义(MAPC FXL) (如果(空L) 升 (cons(f x(car l))(mapc f x(cdr l)))))
也许这样? (另)
(define (power-set A)
(cond
[(null? A) '()] ; the power set of an empty set it empty
[else (append (map (lambda (S) (cons x S)) ; sets with x
(power-set (cdr A)))
(power-set (cdr A)) ; sets without x
]))
这本质上是 '组合' 功能(https://docs.racket-lang.org/reference/pairs.html?q=combinations#%28def._%28%28lib._racket%2Flist..rkt%29._combinations%29%29)。
继球拍(A计划衍生物)短代码获取所有组合或部分组成:
(define (myCombinations L)
(define ol (list L)) ; Define outlist and add full list as one combination;
(let loop ((L L)) ; Recursive loop where elements are removed one by one..
(for ((i L)) ; ..to create progressively smaller combinations;
(define K (remove i L))
(set! ol (cons K ol)) ; Add new combination to outlist;
(loop K)))
(remove-duplicates ol))
测试:
(myCombinations '(a b c))
输出:
'(() (a) (b) (a b) (c) (a c) (b c) (a b c))
那么它不工作,它返回'() –