Sha256: 6b13dc4dbfedd62bba71506228bf16c8de4fbd3165e46a6bf64e34c3f1d2cd58

Contents?: true

Size: 674 Bytes

Versions: 20

Compression:

Stored size: 674 Bytes

Contents

module AllYourBase

let toBase b n =
    let rec loop n acc =
        if n = 0 then acc else
        let digit, n' = n % b, n / b
        loop n' (digit::acc)
    match loop n [] with
    | [] -> Some [0]
    | digits -> Some digits

let fromBase b nums =
    let rec loop acc = function
    | [] -> Some acc
    | digit::rest ->
        if digit <  0 then None else
        if digit >= b then None else
        loop (acc * b + digit) rest

    match nums with
    | [] | 0::_ -> None
    | _ -> loop 0 nums

let rebase inB inDigits outB =
    if inB < 2 || outB < 2 then None else
    match fromBase inB inDigits with
    | None -> None
    | Some num -> toBase outB num

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
trackler-2.2.1.64 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.63 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.62 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.61 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.60 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.59 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.58 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.57 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.56 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.55 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.54 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.53 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.52 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.51 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.50 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.49 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.48 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.47 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.46 tracks/fsharp/exercises/all-your-base/Example.fs
trackler-2.2.1.45 tracks/fsharp/exercises/all-your-base/Example.fs