query
On this page

toBaseDigits

lib.trivial.toBaseDigits

Docs pulled from | This Revision | 10 minutes ago


toBaseDigits base i converts the positive integer i to a list of its digits in the given base. For example:

toBaseDigits 10 123 => [ 1 2 3 ]

toBaseDigits 2 6 => [ 1 1 0 ]

toBaseDigits 16 250 => [ 15 10 ]

Inputs

base

1. Function argument

i

2. Function argument


Noogle detected

Aliases

Implementation

The following is the current implementation of this function.

toBaseDigits =
    base: i:
    let
      go =
        i:
        if i < base then
          [ i ]
        else
          let
            r = i - ((i / base) * base);
            q = (i - r) / base;
          in
          [ r ] ++ go q;
    in
    assert (isInt base);
    assert (isInt i);
    assert (base >= 2);
    assert (i >= 0);
    lib.reverseList (go i);