Last active
June 23, 2018 05:57
-
-
Save justinmeiners/b2133f470a863a3cec5746a584c3f977 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use srfi-1) | |
(define (advance ls n) | |
(if (< n 1) | |
ls | |
(advance (cdr ls) (- n 1)))) | |
(define (subset ls start count) | |
(define (build-cell it i) | |
(if (= i count) | |
'() | |
(cons (car it) (build-cell (cdr it) (+ i 1))))) | |
(build-cell (advance ls start) 0)) | |
(define (cartesian-product list-a list-b) | |
(append-map | |
(lambda (a) | |
(map (lambda (b) | |
(list a b)) list-b)) list-a)) | |
(define (associations items) | |
(define (build-sub k n all) | |
(if (= k n) | |
all | |
(let ((left-set (associations (subset items 0 k))) | |
(right-set (associations (subset items k (- n k))))) | |
(build-sub (+ k 1) n (append (cartesian-product left-set right-set) all))))) | |
(let ((n (length items))) | |
(if (<= n 1) | |
items | |
(build-sub 1 n '())))) | |
(define (input-loop) | |
(begin | |
(display "enter the list of symbols to associate: ") | |
(map (lambda (x) | |
(display x) | |
(newline)) | |
(associations (read))) | |
(input-loop))) | |
(input-loop) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment