How to do a powerset in DrRacket?

后端 未结 5 761
予麋鹿
予麋鹿 2020-11-28 14:41

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

5条回答
  •  感情败类
    2020-11-28 15:11

    Here's my implementation of power set (though I only tested it using standard Racket language, not Beginning Student):

    (define (powerset lst)
      (if (null? lst)
          '(())
          (append-map (lambda (x)
                        (list x (cons (car lst) x)))
                      (powerset (cdr lst)))))
    

    (Thanks to samth for reminding me that flatmap is called append-map in Racket!)

提交回复
热议问题