]> git.rkrishnan.org Git - sicp.git/blobdiff - src/sicp/distinct.rkt
solutions to 4.38, 4.39 and 4.40
[sicp.git] / src / sicp / distinct.rkt
diff --git a/src/sicp/distinct.rkt b/src/sicp/distinct.rkt
new file mode 100644 (file)
index 0000000..3f2e2e0
--- /dev/null
@@ -0,0 +1,26 @@
+#lang racket
+
+(provide distinct?)
+
+(define (distinct? xs)
+  (let loop [(s (set))
+             (r xs)]
+    (cond [(empty? r) #t]
+          [(set-member? s (first r)) #f]
+          [else (loop (set-add s (first r)) (rest r))])))
+
+(module+ test
+  (require rackunit)
+  
+  (check-equal? (distinct? '()) #t)
+  (check-equal? (distinct? '(1)) #t)
+  (check-equal? (distinct? '(1 2)) #t)
+  (check-equal? (distinct? '(1 1)) #f)
+  (check-equal? (distinct? '(1 2 3)) #t)
+  (check-equal? (distinct? '(1 2 3 3 2)) #f)
+  (check-equal? (distinct? '(a b)) #t)
+  (check-equal? (distinct? '(a b a)) #f)
+  (check-equal? (distinct? '(a b c c)) #f)
+  (check-equal? (distinct? '(1 2 3 4)) #t)
+  (check-equal? (distinct? '(1 2 3 4 5)) #t)
+  (check-equal? (distinct? '(1 (2 3) 4 2 3)) #t))
\ No newline at end of file