如何在DrRacket中进行电源设置? [英] How to do a powerset in DrRacket?

查看:139
本文介绍了如何在DrRacket中进行电源设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有DrRacket列表缩写的开始语言,并且想递归地创建一个Powerset,但无法弄清楚该怎么做.我目前有这么多

I'm using the beginning language with list abbreviations for DrRacket and want to make a powerset recursively but cannot figure out how to do it. I currently have this much

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

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

这篇关于如何在DrRacket中进行电源设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆