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