练习 2.32

完整的程序定义如下:

;;; 32-subsets.scm

(define (subsets s)
    (if (null? s)
        (list '())
        (let ((rest (subsets (cdr s))))
            (append rest (map (lambda (x)
                                (cons (car s) x))
                              rest)))))

测试:

1 ]=> (load "32-subsets.scm")

;Loading "32-subsets.scm"... done
;Value: subsets

1 ]=> (subsets (list 1 2 3))

;Value 11: (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3))

这个过程的原理和之前的找零程序的原理是一样的,任何一本组合数学方面的书都能找到相关的资料。

讨论

blog comments powered by Disqus