Convert seconds to compound duration: Difference between revisions

Content deleted Content added
Hout (talk | contribs)
→‎{{header|Haskell}}: Or, parameterising the local names for these durations:
Line 1,244: Line 1,244:
Or, parameterising the local names for these durations:
Or, parameterising the local names for these durations:


<lang haskell>import Data.List (intercalate)
<lang haskell>import Data.List (intercalate, mapAccumR)
timePhrase :: [String] -> Int -> String
timePhrase :: [String] -> Int -> String
Line 1,258: Line 1,258:
weekParts :: Int -> [Int]
weekParts :: Int -> [Int]
weekParts n = xs where
weekParts n = xs where
(xs, _) = foldl byUnits ([], n) [60, 60, 24, 7, 0]
(_, xs) = mapAccumR byUnits n [0, 7, 24, 60, 60]
byUnits :: ([Int], Int) -> Int -> ([Int], Int)
byUnits :: Int -> Int -> (Int, Int)
byUnits (parts, rest) x =
byUnits rest x =
([m] ++ parts, quot (rest - m) u) where
(quot (rest - m) u, m) where
(u, m) | x > 0 = (x, rem rest x)
(u, m) | x > 0 = (x, rem rest x)
| otherwise = (1, rest )
| otherwise = (1, rest )
Line 1,275: Line 1,275:


main :: IO ()
main :: IO ()
main = putStr $ intercalate "\n" $
main = mapM_ putStrLn $
translation <$> [7259, 86400, 6000000]</lang>
translation <$> [7259, 86400, 6000000]</lang>