gpt4 book ai didi

scheme - 如何在 DrRacket 中进行 powerset?

转载 作者:行者123 更新时间:2023-12-04 09:40:40 25 4
gpt4 key购买 nike

我正在使用带有 DrRacket 列表缩写的起始语言,并且想要递归地创建一个 powerset,但无法弄清楚如何去做。我目前有这么多

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

任何帮助都会很好。

最佳答案

            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
            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 when 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`
“没有答案,只有选择”。相当,
做出的选择,就是答案的构成。

关于scheme - 如何在 DrRacket 中进行 powerset?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20622945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com