怎么在DrRacket做一个powerset?

use*_*171 7 scheme powerset racket

我正在使用DrRacket的列表缩写的开头语言,并希望递归地制作一个powerset但是无法弄清楚如何去做.我现在有这么多

(define
  (powerset aL)
  (cond
    [(empty? aL) (list)]
Run Code Online (Sandbox Code Playgroud)

任何帮助都会很好.

Wil*_*ess 10

            What's in a powerset? A set's subsets! 
            An empty set is any set's subset,
            so powerset of empty set's not empty. 
            Its (only) element it is an empty set:

(define
  (powerset aL)
  (cond
    [(empty? aL) (list empty)]
    [else
Run Code Online (Sandbox Code Playgroud)

            As for non-empty sets, there is a choice,
            for each set's element, whether to be
            or not to be included in subset
            which is a member of a powerset. 
We thus include
both choices by combining first element with smaller powerset, that, which we get recursively applying the same procedure to the rest of input:

       (combine (first aL)
                (powerset (rest aL)))]))

(define
  (combine a r)                         ; `r` for Recursive Result
  (cond
    [(empty? r)  empty]                 ; nothing to combine `a` with
    [else
      (cons (cons a (first r))          ; Both add `a` and
            (cons (first r)             ;   don't add, to first subset in `r`
                  (combine              ; and do the same
                      a                 ;   with 
                      (rest r))))]))    ;   the rest of `r`
Run Code Online (Sandbox Code Playgroud)

            "There are no answers, only choices". Rather, 
            the choices made, are what the answer's made of.


soe*_*ard 6

在球拍中,

#lang racket

(define (power-set xs)
  (cond
    [(empty? xs) (list empty)]                 ; the empty set has only empty as subset
    [(cons? xs)  (define x  (first xs))        ; a constructed list has a first element
                 (define ys (rest  xs))        ; and a list of the remaining elements
                 ;; There are two types of subsets of xs, thouse that
                 ;; contain x and those without x.
                 (define with-out-x            ; the power sets without x
                   (power-set ys))                 
                 (define with-x                ; to get the power sets with x we 
                   (cons-all x with-out-x))    ; we add x to the power sets without x
                 (append with-out-x with-x)])) ; Now both kind of subsets are returned.

(define (cons-all x xss)
  ; xss is a list of lists
  ; cons x onto all the lists in xss
  (cond
    [(empty? xss) empty]
    [(cons?  xss) (cons (cons     x (first xss))    ; cons x to the first sublist
                        (cons-all x (rest xss)))])) ; and to the rest of the sublists
Run Code Online (Sandbox Code Playgroud)

去测试:

(power-set '(a b c))
Run Code Online (Sandbox Code Playgroud)


Chr*_*ung 3

这是我的 power set 实现(尽管我只使用标准 Racket 语言而不是 Beginning Student 对其进行了测试):

(define (powerset lst)
  (if (null? lst)
      '(())
      (append-map (lambda (x)
                    (list x (cons (car lst) x)))
                  (powerset (cdr lst)))))
Run Code Online (Sandbox Code Playgroud)

(感谢samth提醒我append-map在 Racket 中调用了 flatmap!)