Tokenize a string: Difference between revisions

Content added Content deleted
(Added Oz example.)
Line 576:
Or the [[:Category:Recursion|tail-recursive]] equivalent:
 
<lang ocaml>let(* split_char[try sep.. strwith] =structures break tail-recursion,
so we externalise it in a sub-function *)
let string_index str c =
try let i = Some(String.index str sep inc)
with Not_found -> None
 
let split_char sep str =
let rec aux acc str =
match string_index str sep with
try
| Some i ->
let i = String.index str sep in
let this = String.sub str 0 i
and next = String.sub str (i+1) (String.length str - i - 1) in
aux (this::acc) next
with Not_found | None ->
List.rev(str::acc)
in
aux [] str