In Standard ML, we have two built-in functions called 'explode' and 'implode':
fun explode: string -> char list
fun implode: char list -> string
So, you get it, 'explode' makes a list of characters from a string and 'implode' does the inverse.
However, OCaml does not have these functions. Regardless; you may derive them like so:
let explode s = List.init (String.length s) (String.get s)
let implode l = String.init (List.length l) (List.nth l)
Enjoy.
Using
List.nth
will give you bad time complexity since list access is linear not random. Prefer this:also OCaml offers some char-by-char iteration functions on
String
directly so that you don't have to convert to an intermediate format: