OCaml lacks conversion between string and char list. #5367
Original bug ID: 5367
Correct me if I'm wrong, but it seems that OCaml 1) does not treat strings as lists of chars and 2) fails to provide built-in functions that convert between strings and char lists. As a result, OCaml makes it difficult to map a char -> something function over a string.
According to the old OCaml website, this functionality can be added with the functions explode and implode:
let explode s =
let implode l =
In the future, can these (or an optimized version of the same) be included in the standard library?
The text was updated successfully, but these errors were encountered:
Comment author: @damiendoligez
The functions explode and implode were in older versions of Caml, but we omitted them from OCaml because they encourage inefficient code. It is generally a bad idea to treat a string as a list of characters, and seeing it as an array of characters is a much better fit to the actual implementation.
Maybe we should provide a map function with the following signature, but even that is a non-obvious decision.
In general, if you feel that some function is missing from the std lib, you should have a look at extended libraries produced by the community: Jane Street Core and Batteries for example. The standard library will remain small.
Comment author: mcandre
Hmm, from a C perspective, it would make sense to treat strings as char arrays rather than char lists, but it's still very useful to provide a built-in way to map a function over all the characters in a string. It's not a matter of efficiency; it's a matter of utility.