summaryrefslogtreecommitdiff
path: root/coding-exercises/2/32.rkt
blob: b7616ac2373820ca824e76e1172a7aba4b7d6242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#lang racket

;; Accumulates all combinations with the car element and without the car element in a list
;; The appends:
;; 1. (()) + ((3))
;; 2. (() (3)) + ((2) (2 3))
;; ...
;;
;; Lisp-wise it works out because the empty set follows from these rules when the input set is empty,
;; so every level introduces the list with the car element itself and combinations with all previous combinations
(define (subsets s)
  (if (null? s)
    (list nil)
    (let ((rest (subsets (cdr s))))
      (append rest (map (lambda (x)
                          (cons (car s) x)) rest)))))
((lambda ()
   (display (subsets (list 1 2 3)))))

(println (cons 2 '()))
(println (list 2))