sumOfDigits.hs
sumOfDigits :: Integer -> Integer sumOfDigits 0 = 0 sumOfDigits num | (num < 0) = sumOfDigits ((-1) * num) | otherwise = (getLastDigit num) + (sumOfDigits (num `div` 10)) {- Return the last digit of a number -} getLastDigit :: Integer -> Integer getLastDigit num | (num < 0) = ((-1) * num) `rem` 10 | otherwise = num `rem` 10
*Main> :load sumOfDigits.hs [1 of 1] Compiling Main ( sumOfDigits.hs, interpreted ) Ok, modules loaded: Main. *Main> *Main> sumOfDigits (-1234) 10 *Main> sumOfDigits 1234 10 *Main> sumOfDigits 1 1 *Main> sumOfDigits 0 0 *Main>
No comments:
Post a Comment