Skip to content

Instantly share code, notes, and snippets.

@justinmeiners
Last active June 23, 2018 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinmeiners/b2133f470a863a3cec5746a584c3f977 to your computer and use it in GitHub Desktop.
Save justinmeiners/b2133f470a863a3cec5746a584c3f977 to your computer and use it in GitHub Desktop.
(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