【问题标题】:Scheme set made from parts of set由集合的部分组成的方案集合
【发布时间】:2017-02-21 12:36:36
【问题描述】:

您好,我正在尝试定义一个函数,该函数应该从该集合的各个部分组成一个集合。 应该定义为:P(A) = P(A-{x}) U { {x} U B} 对于所有属于 P(A-{X}) 的 B,其中 X 属于 A。

测试将是:

(部分'(a b c)) => ((a b c) (a b) (a c) (a) (b c) (b) (c)())

我一直在尝试这个:

(定义 (mapc f x l) (如果(空?l) l (缺点 (f x (car l)) (mapc f x (cdr l)))))

【问题讨论】:

    标签: set scheme


    【解决方案1】:

    也许是这样的? (未经测试)

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

    【讨论】:

      【解决方案2】:

      这本质上是“组合”功能 (https://docs.racket-lang.org/reference/pairs.html?q=combinations#%28def._%28%28lib._racket%2Flist..rkt%29._combinations%29%29)。

      以下 Racket 中的短代码(Scheme 派生)获取所有组合或部分:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-19
        • 1970-01-01
        • 2018-01-03
        相关资源
        最近更新 更多