Sha256: a89cd0d503d3624181f2f492997ee303eff2cd31fc0e06ccf260786cb36c22d8

Contents?: true

Size: 594 Bytes

Versions: 107

Compression:

Stored size: 594 Bytes

Contents

open Core

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

107 entries across 107 versions & 1 rubygems

Version Path
trackler-2.2.1.46 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.45 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.44 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.43 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.42 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.41 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.40 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.39 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.38 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.37 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.36 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.35 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.34 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.33 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.32 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.31 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.30 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.29 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.28 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.27 tracks/ocaml/exercises/all-your-base/example.ml