# klutometis/combinatorics

### Subversion checkout URL

You can clone with
or
.
Fetching contributors…

Cannot retrieve contributors at this time

183 lines (174 sloc) 7.43 KB

combinatorics

Combinatorics toc:

= Abstract

provides some mechanisms for iterating over, reducing and mapping permutations (ordered subsets) and combinations (unordered subsets) of lists.

supports partial, i.e. k-permutations and partial, i.e. k-combinations.

= Documentation

<procedure>(ordered-subset-for-each f list) → unspecified</procedure> <procedure>(ordered-subset-for-each f list k) → unspecified</procedure> Iterate over every k-permutation (partial ordered subset) of , calling for its side effect.

: The function to call
: The list to permute
: k distinct elements (default: n)
<enscript highlight="scheme">(define ordered-subset-for-each (case-lambda ((f list) (ordered-subset-for-each f list (length list))) ((f list k) (let iter ((list list) (k k) (subset '())) (if (zero? k) (f subset) (for-each (lambda (element) (iter (delete element list) (sub1 k) (cons element subset))) list)))))) </enscript>

<procedure>(ordered-subset-fold cons nil list) → object</procedure> <procedure>(ordered-subset-fold cons nil list k) → object</procedure> Recombine every k-permutation (partial ordered subset) of , starting with a base-case , and calling with 1. a permutation and 2. the accumulated value.

: The combining function
: The base case
: The list to recombine
: k distinct elements (default: n)
<enscript highlight="scheme">(define ordered-subset-fold (case-lambda ((cons nil list) (ordered-subset-fold cons nil list (length list))) ((cons nil list k) (let ((nil (make-parameter nil))) (ordered-subset-for-each (lambda (subset) (nil (cons subset (nil)))) list k) (nil))))) </enscript>

<procedure>(ordered-subset-map f list) → list</procedure> <procedure>(ordered-subset-map f list k) → list</procedure> Map every k-permutation (partial ordered subset) of using .

: The mapping function
: The list to map
: k distinct elements (default: n)
<enscript highlight="scheme">(define ordered-subset-map (case-lambda ((f list) (ordered-subset-map f list (length list))) ((f list k) (ordered-subset-fold (lambda (v a) (cons (f v) a)) '() list k)))) </enscript>

<procedure>(unordered-subset-for-each f list) → unspecified</procedure> <procedure>(unordered-subset-for-each f list k) → unspecified</procedure> Iterate over every k-combination (partial unordered subset) of , calling for its side effect.

: The function to call
: The list to permute
: k distinct elements (default: n)
<enscript highlight="scheme">(define unordered-subset-for-each (case-lambda ((f list) (unordered-subset-for-each f list (length list))) ((f list k) (let ((subset (make-vector k)) (n (length list))) (let iter ((start 0) (p 0)) (if (= p k) (f (project subset list)) (do ((i start (+ i 1))) ((= i n)) (vector-set! subset p i) (iter (add1 i) (add1 p))))))))) </enscript>

<procedure>(unordered-subset-fold cons nil list) → object</procedure> <procedure>(unordered-subset-fold cons nil list k) → object</procedure> Recombine every k-combination (partial unordered subset) of , starting with a base-case , and calling with 1. a combination and 2. the accumulated value.

: The combining function
: The base case
: The list to recombine
: k distinct elements (default: n)
<enscript highlight="scheme">(define unordered-subset-fold (case-lambda ((cons nil list) (unordered-subset-fold cons nil list (length list))) ((cons nil list k) (let ((nil (make-parameter nil))) (unordered-subset-for-each (lambda (subset) (nil (cons subset (nil)))) list k) (nil))))) </enscript>

<procedure>(unordered-subset-map f list) → list</procedure> <procedure>(unordered-subset-map f list k) → list</procedure> Map every k-combination (partial unordered subset) of using .

: The mapping function
: The list to map
: k distinct elements (default: n)
<enscript highlight="scheme">(define unordered-subset-map (case-lambda ((f list) (unordered-subset-map f list (length list))) ((f list k) (unordered-subset-fold (lambda (v a) (cons (f v) a)) '() list k)))) </enscript>

<constant>permutation-for-each → ordered-subset-for-each</constant> Synonym for #ordered-subset-for-each <enscript highlight="scheme">(define permutation-for-each ordered-subset-for-each) </enscript>

<constant>permutation-fold → ordered-subset-fold</constant> Synonym for #ordered-subset-fold <enscript highlight="scheme">(define permutation-fold ordered-subset-fold) </enscript>

<constant>permutation-map → ordered-subset-map</constant> Synonym for #ordered-subset-map <enscript highlight="scheme">(define permutation-map ordered-subset-map) </enscript>

<constant>combination-for-each → unordered-subset-for-each</constant> Synonym for #unordered-subset-for-each <enscript highlight="scheme">(define combination-for-each unordered-subset-for-each) </enscript>

<constant>combination-fold → unordered-subset-fold</constant> Synonym for #unordered-subset-fold <enscript highlight="scheme">(define combination-fold unordered-subset-fold) </enscript>

<constant>combination-map → unordered-subset-map</constant> Synonym for #unordered-subset-map <enscript highlight="scheme">(define combination-map unordered-subset-map) </enscript>

BSD

Dependencies Versions
0.2 : Add unordered subset operations.