I want to calculate the sum of the digits of a number in a Scheme. It should work as follows:
>(sum-of-digits 123)
6
My idea is to convert a number 123to a string "123", and then convert it to a list '(1 2 3), and then use (apply + '(1 2 3))to get 6.
but it, unfortunately, does not work, as I imagined.
>(string->list(number->string 123))
'(#\1 #\2 #\3)
Apparently '(#\1 #\2 #\3)not the same as '(1 2 3)... because I use the language racketin DrRacket, so I cannot use a type function char->digit.
Can someone help me fix this?
source
share