Skip to content

Instantly share code, notes, and snippets.

@cushon
Created March 21, 2011 01:17
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 cushon/878867 to your computer and use it in GitHub Desktop.
Save cushon/878867 to your computer and use it in GitHub Desktop.
Does a list contain duplicate references?
(define (list-uniq l)
(define t (make-hasheq))
(define (check l)
(or (empty? l)
(not (cons? l))
(and (cons? l)
(not (hash-ref t l #f))
(begin
(hash-set! t l #t)
(check/list l)))))
(define (check/list l)
(or (empty? l)
(and (check (car l))
(check/list (cdr l)))))
(check l))
(define a '(1 (2 (3 4)) 5))
(list-uniq a) ;#t
(list-uniq (list a a)) ;#f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment