Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
let char_0 = Char.code '0'
let split n s =
let len_s = String.length s in
let res = Array.make n 0 in
let rec slide pos =
if pos < len_s && s.[pos] <> ' ' then
pos + 1 |> slide
else pos
in
let rec to_int i over n =
if i < over then
let n = n * 10 + Char.code s.[i] - char_0 in
to_int (i + 1) over n
else
n
in
let rec loop i j =
if i < n && j < len_s then
let j' = slide j in
if s.[j] = '-' then
res.(i) <- -1 * to_int (j + 1) j' 0
else
res.(i) <- to_int j j' 0;
loop (i + 1) (j' + 1)
in
loop 0 0;
res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment