Skip to content

Instantly share code, notes, and snippets.

@d11wtq
Created June 18, 2012 14:32
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 d11wtq/2948661 to your computer and use it in GitHub Desktop.
Save d11wtq/2948661 to your computer and use it in GitHub Desktop.
An exercise from The Seasoned Schemer. I really like the shape and feel of this.
; Given two sets, return a new set containing the unique members from each.
(define (union set1 set2)
(letrec
((union
(lambda (set)
(cond
((null? set) set2)
((member? (car set) set2)
(union (cdr set)))
(else
(cons (car set)
(union (cdr set)))))))
(member?
(lambda (a set)
(cond
((null? set) #f)
((equal? (car set) a) #t)
(else
(member? a (cdr set)))))))
(union set1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment