Sha256: 62a6ce6350f294a0297fa327bd9fbafb93a7eb4c19039fa8b2c60be67e584556

Contents?: true

Size: 570 Bytes

Versions: 68

Compression:

Stored size: 570 Bytes

Contents

open Base

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
  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

68 entries across 68 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.179 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.178 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.177 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.176 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.175 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.174 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.173 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.172 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.171 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.170 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.169 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.167 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.166 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.165 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.164 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.163 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.162 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.161 tracks/ocaml/exercises/all-your-base/example.ml
trackler-2.2.1.160 tracks/ocaml/exercises/all-your-base/example.ml