I'm looking for a non-recursive implementation of sum of digits (a "cross sum") of a non-negative number like this:
cs :: Int -> Int
cs n = sum digits_of_n where digits_of_n = [ . | ... ]
A cross sum of a number (e.g. 512) is the sum of its individual digits (e.g. 5 + 1 + 2 = 8)
cs takes a non-negative number "n" as input, then should use list comprehension to split the number to its digits (e.g. 1234 -> [1,2,3,4]), which then gets summed up.
The part where list comprehension is used is the question, I don't know how to implement that.
The "usual", recursive way would be extracting the digits from a number recursively using modulo and division, and then summing them up like this:
cs :: Int -> Int
cs n = if n == 0 then 0 else n `mod` 10 + cs (n `div` 10)
I have however difficulty expressing this without recursion and with list comprehension, does anyone have ideas regarding this?