Use list as set

Problem

You want to add element to the list, but make all elements unique. So it work like set.

Solution

(define (adjoin x a)
  (if (member x a)
      a
      (cons x a)))

Credit: Nils M Holm (ref: adjoin.scm)

Usage

(adjoin 'x '(a b c))
;; ==> (x a b c)

(adjoin 'c '(a b c))
;; ==> (a b c)

Back to the Scheme Cookbook