0 Replies - 1031 Views - Last Post: 13 April 2010 - 05:57 PM

#1 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

[Lisp] Sum of Digits in Clojure

Posted 13 April 2010 - 05:57 PM

Description: Don't pass it a floating point number, it'll only work with integersRecursively sums up the digits in an integer
(defn sum-digits
  "returns the sum of the digits: 1234 = 1 + 2 + 3 + 4 = 10"
  [num]
  (cond
    (zero? num) 0
    :else  (+ (mod num 10) (sum-digits (int (/ num 10))))))

Is This A Good Question/Topic? 0
  • +

Page 1 of 1