嗨,我試圖定義一個函數,應該從該集合的部分進行設置。 對於所有屬於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))
那麼它不工作,它返回'() –