Any O'Caml hackers in Sluggadonia?
I'm writing a function to split a string into a list on a specific character,
in a purely functional and tail-recursive way.
So far I have:
let split sep s =
let pair start =
try
let pos = String.index_from s start sep in
(String.sub s start (pos - start), pos + 1)
with Not_found -> (String.sub s start ((String.length s) -
start), -1) in
let rec loop start accu =
let chunk, pos = pair start in
if pos == -1 then
chunk :: accu
else
loop pos (chunk :: accu) in
loop 0 []
I'm trying to figure out if that is optimal or not.
(Yes, I'm aware it returns a list of items in the reverse order,
that's not a problem).
-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS). Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.
This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 20:37:09 EDT