Sum of digits in Clojure I wrote a simple program to sum the digits of a number in Clojure for learning purpose. Not very complex (I hope) so not adding any explanation. I am a beginner in Clojure so please don't mind pointing out even the most obvious mistakes.

NOTE: It recursively calculates the sum of digits in the result until a single digit result is obtained. For example, given "4312", 1 is returned (4312 -> 10 -> 1).



(defn sum-once [x]
  (reduce +
          (map
            #(Integer/parseInt (str %))
            (seq (char-array x)))))

(defn sum-digits [x]
  (let [y (sum-once x)]
    (if (&lt; y 10) y
                 (sum-digits (str y)))))
I was confused by your code until reading the comments. You want to change the name of sum-once to sum-digits or digit-sum (pick this one!) or anything like it, since that's what it really does.

Your current sum-digits should become digital-root (that's the term for what you're trying to achieve).


  The digital root (also repeated digital sum) of a non-negative integer
  is the (single digit) value obtained by an iterative process of
  summing digits, on each iteration using the result from the previous
  iteration to compute a digit sum. The process continues until a
  single-digit number is reached.