##### 问题描述投票：-1回答：3

``````import System.IO
import Data.List

integer = 888

todigits :: Integral x => x -> [x]
todigits 0 = []
todigits x = todigits (x `div` 10) ++ [x `mod` 10]

add (x:xs) = sum(x:xs)

main = do
``````
##### 3个回答
2

``````todigitsOnce :: Integral x => x -> [x]
todigitsOnce 0 = []
todigitsOnce x = x `mod` 10 : todigitsOnce (x `div` 10)
``````

``````toDigit :: Integral x => x -> [x]
toDigit x
| length firstResult < 2 = firstResult
| otherwise = toDigit . sum \$ firstResult
where firstResult = todigitsOnce x
``````

3

``````digitSum x = case (x, x `mod` 9) of
(0, _) -> 0
(_, 0) -> 9
(_, v) -> v
``````

``````> digitSum 888
6
``````

2

``````{-# LANGUAGE ViewPatterns #-}

digitSum :: Int -> Int
digitSum x@(show -> (_:"")) = x
digitSum (show -> cs) = digitSum \$ sum . map ( read . (:[]) ) \$ cs
``````

``````import Data.List (unfoldr)
import Data.Tuple (swap)

digitList :: (Integral a) => a -> [a]
digitList 0 = [0]
digitList n = unfoldr f n
where f 0 = Nothing
f i = Just . swap \$ i `divMod` 10

digitSum' :: (Integral a) => a -> a
digitSum' (digitList -> x:[]) = x
digitSum' (digitList -> xs) = digitSum' \$ sum xs
``````