Scheme 如何在DrRacket中进行动力设置?

Scheme 如何在DrRacket中进行动力设置?,scheme,racket,powerset,htdp,Scheme,Racket,Powerset,Htdp,我使用的是DrRacket的开头语言和列表缩写,我想递归地创建一个powerset,但不知道怎么做。我现在有这么多 (define (powerset aL) (cond [(empty? aL) (list)] 任何帮助都会很好。下面是我对power set的实现(尽管我只使用标准的Racket语言测试了它,而不是初学者): (感谢您提醒我flatmap在Racket中被称为append map) “没有答案,只有选择”。相当地 所做的选择就是答案的组成部分。这里是另一个实现

我使用的是DrRacket的开头语言和列表缩写,我想递归地创建一个powerset,但不知道怎么做。我现在有这么多

(define
  (powerset aL)
  (cond
    [(empty? aL) (list)]

任何帮助都会很好。

下面是我对power set的实现(尽管我只使用标准的Racket语言测试了它,而不是初学者):

(感谢您提醒我flatmap在Racket中被称为
append map

“没有答案,只有选择”。相当地
所做的选择就是答案的组成部分。这里是另一个实现,经过几次测试后,它似乎比Chris的答案更快地给出更大的列表。使用标准球拍测试:

(define (powerset aL)
  (if (empty? aL)
      '(())
      (let ((rst (powerset (rest aL))))
        (append (map (lambda (x) (cons (first aL) x))
                     rst)
                rst))))
在球拍上

#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
要测试:

(power-set '(a b c))

您可以只使用副作用:

(define res '())

(define
  (pow raw leaf)
  (cond
    [(empty? raw) (set! res (cons leaf res))
                  res]
    [else (pow (cdr raw) leaf)
          (pow (cdr raw) (cons (car raw) leaf))]))

(pow '(1 2 3) '())

(require racket/list)(定义powerset组合)
是的,这是我编写的原始实现(因为它比我最终发布的版本更简单,因此直观地浮现在我的脑海中),但我不喜欢结果元素的顺序。(我知道,从什么时候开始,集合是关于排序的,对吗?)仍然,有一个+1.:-)我喜欢这个解决方案,它比我发布的更简单,这是一个展示
append map
用法的好例子。我想知道,为什么它慢一点?没有
设置在中。但这很简单。它可以在DrRacket上运行。问题是“我正在使用列表缩写的开头语言”。我投了赞成票好的,谢谢你的更正。我也投了你的票。
(define (powerset aL)
  (if (empty? aL)
      '(())
      (let ((rst (powerset (rest aL))))
        (append (map (lambda (x) (cons (first aL) x))
                     rst)
                rst))))
#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
(power-set '(a b c))
(define res '())

(define
  (pow raw leaf)
  (cond
    [(empty? raw) (set! res (cons leaf res))
                  res]
    [else (pow (cdr raw) leaf)
          (pow (cdr raw) (cons (car raw) leaf))]))

(pow '(1 2 3) '())