I want to calculate the sum of digits of a number in Scheme. It should work like this:
>(sum-of-digits 123)
6
My idea is to transform the number 123
to string "123"
and then transform it to a list '(1 2 3)
and then use (apply + '(1 2 3))
to get 6
.
but it's unfortunately not working like I imagined.
>(string->list(number->string 123))
'(#\1 #\2 #\3)
Apparently '(#\1 #\2 #\3)
is not same as '(1 2 3)
... because I'm using language racket
under DrRacket, so I can not use the function like char->digit
.
Can anyone help me fix this?