aboutsummaryrefslogtreecommitdiff
path: root/combinations.lisp
diff options
context:
space:
mode:
authorMasaya Tojo <masaya@tojo.tokyo>2021-09-01 23:12:41 +0900
committerMasaya Tojo <masaya@tojo.tokyo>2021-09-01 23:12:41 +0900
commit975418f4c2e1520649196458d3211e6ef9ec531a (patch)
treee0b4b79b0755a38db1a15d4561b41ecfdad3442c /combinations.lisp
parent279063668f17a09d5f272af1d1614f63cc323cd9 (diff)
Add combinations.lisp file.
Diffstat (limited to 'combinations.lisp')
-rw-r--r--combinations.lisp28
1 files changed, 28 insertions, 0 deletions
diff --git a/combinations.lisp b/combinations.lisp
new file mode 100644
index 0000000..1867923
--- /dev/null
+++ b/combinations.lisp
@@ -0,0 +1,28 @@
+(defun cons-all (e x)
+ (declare (xargs :guard (true-listp x)))
+ (if (endp x)
+ nil
+ (cons (cons e (car x))
+ (cons-all e (cdr x)))))
+
+(defun combinations (x n)
+ (declare (xargs :guard (and (true-listp x)
+ (natp n))))
+ (cond
+ ((zp n) (list nil))
+ ((endp x) nil)
+ (t
+ (append (cons-all (car x)
+ (combinations (cdr x) (- n 1)))
+ (combinations (cdr x) n)))))
+
+(defun comb (n k)
+ (cond
+ ((zp k) 1)
+ ((zp n) 0)
+ (t (+ (comb (- n 1) k)
+ (comb (- n 1) (- k 1))))))
+
+(defthm len-combinations
+ (equal (len (combinations x n))
+ (comb (len x) n)))