RosettaCodeData/Task/Non-decimal-radices-Convert/Standard-ML/non-decimal-radices-convert.ml
Ingy döt Net b83f433714 tasks a-s
2013-04-10 23:57:08 -07:00

25 lines
642 B
Standard ML

fun toBase b v = let
fun toBase' (a, 0) = a
| toBase' (a, v) = toBase' (v mod b :: a, v div b)
in
toBase' ([], v)
end
fun fromBase b ds =
foldl (fn (k, n) => n * b + k) 0 ds
val toAlphaDigits = let
fun convert n = if n < 10 then chr (n + ord #"0")
else chr (n + ord #"a" - 10)
in
implode o map convert
end
val fromAlphaDigits = let
fun convert c = if Char.isDigit c then ord c - ord #"0"
else if Char.isUpper c then ord c - ord #"A" + 10
else if Char.isLower c then ord c - ord #"a" + 10
else raise Match
in
map convert o explode
end