# A Simple Radix in ML

Reading through Stephen Gilmore’s *Programming in Standard ML ‘97: An On-line Tutorial*^{1} this weekend, I found a interesting function. It was a simple `radix`

function for non-negative numbers.

```
val rec radix = fn (n, base) =>
val b = size base
val digit = fn n => str (String.sub (base, n))
val radix' =
fn (true, n) => digit n
| (false, n) => radix (n div b, base) ^ digit (n mod b)
in
radix' (n < b, n)
end;
```

Calling this with a base as the second argument converts a base10 integer to the new base. For example:

`radix(15, "01");`

converts 15 to binary and produces`"1111"`

`radix(255, "0123456789abcdef");`

converts 255 to hexadecimal and produces`"ff"`

Nice!