Jump to content

Convert seconds to compound duration: Difference between revisions

simplify, make clear that helper functions aren't closures
m (→‎{{header|Haskell}}: Tidied parameterised variant.)
(simplify, make clear that helper functions aren't closures)
Line 3,500:
 
=={{header|Standard ML}}==
<lang sml>fun compoundDuration seconds =local
fun fmtNonZero (0, _, list) = list
let
fun| fmtNonZero (0n, s, list) = NONEInt.toString n ^ " " ^ s :: list
fun divModHead (_, []) = []
| fmtNonZero (x, s) = SOME (Int.toString x ^ " " ^ s)
| divModHead (d, head :: tail) = head div d :: head mod d :: tail
 
in
fun divMod (divisor, (duration, list)) =
fun compoundDuration seconds =
(duration div divisor, duration mod divisor :: list)
let
 
val (weeks, subUnits)val digits = foldl divMod (seconds,divModHead [seconds]) [60, 60, 24, 7]
oand ListPair.zip)units (weeks :: subUnits,= ["wk", "d", "hr", "min", "sec"])
in
in
(String.concatWith ", "
oString.concatWith List", " (ListPair.mapPartialfoldr fmtNonZero [] (digits, units))
end
o ListPair.zip) (weeks :: subUnits, ["wk", "d", "hr", "min", "sec"])
end
 
val () = app (fn s => print (compoundDuration s ^ "\n")) [7259, 86400, 6000000]</lang>
559

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.