4 ms·
Wow, Tongan number system is simply beautiful. Pure elegance that I'm sure could be defined mathematically very simply. I myself can only scribble a naive imple
by tekkk 9y ago
Wow, Tongan number system is simply beautiful. Pure elegance that I'm sure could be defined mathematically very simply. I myself can only scribble a naive implementation for natural numbers in Javascript:
const base = ["noa", "taha", "ua", "tolu", "fa", "nima", "ono", "fitu", "valu", "hiva"]
const tongan = n => Array.from(n.toString()).map(Number).map(i => base[i]).join(" ")
Not as pretty as I thought. Probably would look better in functional language.
- FullyFunctional 9y agoI theorize that humans do poorly with too much regularity and we actually benefit from irregularity, unlike computers. In Haskell, first line would be the same without the const and the other could be tongan = intercalate " ".reverse.map ((base !!).(`mod` 10)).takeWhile (> 0).iterate (`div` 10) (ISTM that there must be a better alternative to takeWhile + iterate).
- kjaer 9y agoI wrote it in Scala below; it's a bit shorter, and I really like the map(base). Getting the digits from a number turns out to be most of the work, and the int->string->int conversion also seems to be the best approach. val base = List("noa", "taha", "ua", "tolu", "fa", "nima", "ono", "fitu", "valu", "hiva") def tongan(n: Int) = n.toString.map(_.asDigit).map(base).mkString(" ")
- kccqzy 9y agoEnglish numbers aren't much harder. englishNumbers n = ten9 !! (n - 1) where infixr 7 <<>> (<<>>) = liftA2 (<>) ten1 = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] ten2 = ten1 <> ["ten", "eleven", "twelve"] <> (["thir", "four"] <> prefixes) <<>> ["teen"] <> (["twen", "thir", "for"] <> prefixes) <<>> ["ty"] <<>> ([[]] <> ten1) where prefixes = ["fif", "six", "seven", "eigh", "nine"] ten3 = ten2 <> ten1 <<>> ["hundred"] <<>> ([[]] <> ten2) ten6 = ten3 <> ten3 <<>> ["thousand"] <<>> ([[]] <> ten3) ten9 = ten6 <> ten3 <<>> ["million"] <<>> ([[]] <> ten6)