I am trying to write a function that evaluates to the number of distinct characters in the input string str. So for example (distinct-char "eeeiicczz")
would return 4. I need some help with my code. This is what I have.
(define string-contains
(lambda (str char)
(if (equal? str "")
#f
(if (char=? (string-ref str 0) char)
#t
(if (not (char=? (string-ref str 0) char))
(string-contains (substring str 1 (string-length str)) char))))))
(define unique-chars
(lambda (str)
(cond
((equal? str "") "")
((equal? (string-length str) 1) (string-ref str 0))
(else
(if (equal? (string-contains (substring str 1 (string-length str)) (string-ref str 0)) #t)
(unique-chars (substring str 1 (string-length str)))
(string-append (substring str 0 1) (substring str 1 (string-length str))))))))
(define distinct-char
(lambda (str)
(string-length (unique-chars str))))
I am limited to using these Built in functions:
(if x y z), (cond ...),
(read)
(string-length x)
(string-ref str x)
(substring x y)
(string-append x y)
(equal? x y), (char=?)
(remainder x y), (quotient x y)
(max ...), (min ...)
(+ x y), (- x y), (* x y), (/ x y)
(> x y), (< x y), (<= x y), (>= x y)
(and x y), (or x y), (not x y)