X-Git-Url: https://git.rkrishnan.org/?p=sicp.git;a=blobdiff_plain;f=src%2Fsicp%2Fdistinct.rkt;fp=src%2Fsicp%2Fdistinct.rkt;h=3f2e2e0b7f1c31a600aba96eedcb8d8788d0f09f;hp=0000000000000000000000000000000000000000;hb=50cef372de91c41185de4fac8a1fc456c978b28a;hpb=07f6c2a14b01e1a1f9c585c8a53086fbb819f8f8 diff --git a/src/sicp/distinct.rkt b/src/sicp/distinct.rkt new file mode 100644 index 0000000..3f2e2e0 --- /dev/null +++ b/src/sicp/distinct.rkt @@ -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