New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OCaml lacks conversion between string and char list. #5367
Comments
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. |
Comment author: @mshinwell The standard library remains small, so this is probably for an external stdlib replacement or overlay. |
Maybe I'm spoiled but the fact that this isn't available from the stdlib is a pretty weird design decision. |
It is not: as mentioned in the comment above #5367 (comment) treating strings as lists of chars encourages inefficient code. See this old thread for more https://inbox.ocaml.org/caml-list/20030302193437.A6487@pauillac.inria.fr/. |
Since OCaml 4.07, you can go through sequences, e.g. |
Did not expect this thread to even get attention due to the age of the last post and feature tag. I'm also a complete idiot in terms of OCaml, I think the the Also @nojb that old thread is out there, I'm super new to OCaml how did you even find that? |
I had a vague memory of reading the post a long time ago. It is not always easy to find what you are looking for, but there are certainly some real gems to be found browsing the |
Original bug ID: 5367
Reporter: mcandre
Status: closed (set by @mshinwell on 2016-12-07T13:54:55Z)
Resolution: won't fix
Priority: normal
Severity: feature
Version: 3.12.0
Category: ~DO NOT USE (was: OCaml general)
Monitored by: earthfront @protz
Bug description
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 rec exp i l =
if i < 0 then l else exp (i - 1) (s.[i] :: l) in
exp (String.length s - 1) [];;
let implode l =
let res = String.create (List.length l) in
let rec imp i = function
| [] -> res
| c :: l -> res.[i] <- c; imp (i + 1) l in
imp 0 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: