Sha256: 9f5f927da5bf7c7928611da62d773629edabac74f8d253ff2c911639ec344f15

Contents?: true

Size: 598 Bytes

Versions: 145

Compression:

Stored size: 598 Bytes

Contents

open Core.Std

type base = int

let rec to_digits (b: base) (acc: int list) = function
  | 0 -> acc
  | n -> to_digits b (n % b :: acc) (n / b)

let convert_bases ~from ~digits ~target =
  if from <= 1 || target <= 1 || List.is_empty digits
  then None
  else
    let open Option.Monad_infix in
    let digits = List.fold_left digits ~init:(Some 0) ~f:(fun acc d ->
        if d < 0 || d >= from
        then None
        else acc >>= (fun acc -> Some (acc * from + d))
      ) in
    Option.map digits ~f:(to_digits target [])
    |> Option.map ~f:(fun xs -> if List.is_empty xs then [0] else xs)

Version data entries

145 entries across 145 versions & 1 rubygems

Version Path
trackler-2.1.0.21 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.20 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.19 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.18 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.17 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.16 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.15 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.14 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.13 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.12 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.11 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.10 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.9 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.8 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.7 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.6 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.5 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.4 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.3 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.1.0.2 tracks/ocaml/exercises/all-your-base/example.ml