Sum digits of an integer: Difference between revisions
Content added Content deleted
Line 2,043: | Line 2,043: | ||
Or, we could write '''sum . fmap digitToInt''', or the equivalent but more efficient fusion of it to a single fold: '''foldr ((+) . digitToInt) 0''' |
Or, we could write '''sum . fmap digitToInt''', or the equivalent but more efficient fusion of it to a single fold: '''foldr ((+) . digitToInt) 0''' |
||
<lang haskell>import Data.Char (digitToInt, intToDigit, isHexDigit) |
<lang haskell>import Data.Char (digitToInt, intToDigit, isHexDigit) |
||
import Data.List (transpose |
import Data.List (transpose) |
||
import Numeric ( |
import Numeric (readInt, showIntAtBase) |
||
------------------ SUM OF INTEGER DIGITS ----------------- |
|||
digitSum :: String -> Int |
digitSum :: String -> Int |
||
Line 2,050: | Line 2,052: | ||
intDigitSum :: Int -> Int -> Int |
intDigitSum :: Int -> Int -> Int |
||
intDigitSum base |
intDigitSum base = |
||
digitSum |
|||
. flip (showIntAtBase base intToDigit) [] |
|||
-------------------------- TESTS ------------------------- |
|||
main :: IO () |
main :: IO () |
||
main = |
main = |
||
mapM_ putStrLn $ |
mapM_ putStrLn $ |
||
unwords |
|||
unwords <$> |
|||
transpose |
<$> transpose |
||
( ( fmap |
|||
((fmap =<< flip justifyRight ' ' . succ . maximum . fmap length) <$> |
|||
=<< flip justifyRight ' ' |
|||
transpose |
|||
. succ |
|||
. maximum |
|||
. fmap length |
|||
) |
|||
<$> transpose |
|||
( [ "Base", |
|||
"Digits", |
|||
"Value", |
|||
"digit string -> sum", |
|||
"integer value -> sum" |
|||
] : |
|||
( ( \(s, b) -> |
|||
let v = readBase b s |
|||
in [ show b, -- base |
|||
show s, -- digits |
|||
show v, -- value |
|||
-- sum from digit string |
|||
show (digitSum s), |
|||
-- sum from base and value |
|||
show (intDigitSum b v) |
|||
] |
|||
) |
|||
<$> [ ("1", 10), |
|||
("1234", 10), |
|||
("fe", 16), |
|||
("f0e", 16) |
|||
] |
|||
) |
|||
) |
|||
) |
|||
where |
where |
||
justifyRight n c |
justifyRight n c = (drop . length) <*> (replicate n c <>) |
||
readBase b s = |
readBase b s = n |
||
where |
|||
let [(n, _)] = readInt b isHexDigit digitToInt s |
|||
[(n, _)] = readInt b isHexDigit digitToInt s</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> Base Digits Value digit string -> sum integer value -> sum |
<pre> Base Digits Value digit string -> sum integer value -> sum |