tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

conversions.tri (611B)


      1 !import base *
      2 !import native_math as N
      3 !export showNumber
      4 
      5 -- Canonical naturals to ASCII decimal; zero has one digit, never an empty list.
      6 -- Divide by ten once per digit, retaining the remainder from that same pass.
      7 -- No decrement-through-every-value loop or separate reversed-decimal counter.
      8 showNumber n@nat? = (case n of
      9   0 = "0"
     10   _ = (let rec
     11     digits remaining suffix = (case remaining of
     12       0 = suffix
     13       _ = (case N.divMod remaining 10 of
     14         (quotient, remainder) = digits quotient (pair (N.add 48 remainder) suffix)
     15         _ = err "invalid division result" t))
     16     in digits n []))