Skip to content

Instantly share code, notes, and snippets.

Created January 18, 2018 16:21
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 anonymous/df7b5009f656df985170d139dc19d781 to your computer and use it in GitHub Desktop.
Save anonymous/df7b5009f656df985170d139dc19d781 to your computer and use it in GitHub Desktop.
Generate and find valid sets scm
(define member?
(lambda (a lat)
(cond
((null? lat)
#f)
((equal? a (car lat))
#t)
(else
(member? a (cdr lat))))))
(member? 'a '(a b c d e f))
(member? 'a '(1 2 3 4 5 6))
(member? '(a b) '(a (a b) c))
(define set?
(lambda (s)
(cond
((null? s)
#t)
((member? (car s) (cdr s))
#f)
(else (set? (cdr s))))))
(set? '(1 2 3 4 5))
(set? '(1 2 3 4 1))
(define catom?
(lambda (x)
(and (not (pair? x)) (not (null? x)))))
(define (paste-to-each-element e l)
(cond
((null? l)
(cons e l))
(else
(cond
((catom? (car l))
(append (list (car l)) (list (cons (car l) (list e))) (paste-to-each-element e (cdr l))))
(else (append (list (car l)) (list (append (car l) (list e))) (paste-to-each-element e (cdr l))))))))
(paste-to-each-element 'd '(a b c))
(paste-to-each-element 'c '(a b (a b)))
(define (build-combinations)
(let ((c '()))
(lambda (element)
(cond
((eqv? element 'give)
c)
((null? c)
(set! c (append (list element) '())))
(else
(set! c (paste-to-each-element element c)))))))
(define combine (build-combinations))
(combine '1)
(combine 'give)
(combine '2)
(combine '3)
(combine '3)
(define (get-valid-sets sets)
(cond
((null? sets)
sets)
((member? (car sets) (cdr sets))
(get-valid-sets (cdr sets)))
((catom? (car sets))
(append (list (car sets)) (get-valid-sets (cdr sets))))
((set? (car sets))
(append (list (car sets)) (get-valid-sets (cdr sets))))
(else
(get-valid-sets (cdr sets)))))
(get-valid-sets (combine 'give))
(get-valid-sets '(1 (1 3) (1 3) (1 3 3) (1 2) (1 2 3) (1 2 3) (1 2 3 3) 2 (2 3) (2 3) (2 3 3) 3 (3 3) 3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment