Skip to content
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

Closed
vicuna opened this issue Oct 2, 2011 · 8 comments
Closed

OCaml lacks conversion between string and char list. #5367

vicuna opened this issue Oct 2, 2011 · 8 comments

Comments

@vicuna
Copy link

vicuna commented Oct 2, 2011

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?

@vicuna
Copy link
Author

vicuna commented Oct 5, 2011

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.
String.map: (char -> 'a) -> string -> 'a array

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.

@vicuna
Copy link
Author

vicuna commented Oct 5, 2011

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.

@vicuna
Copy link
Author

vicuna commented Dec 7, 2016

Comment author: @mshinwell

The standard library remains small, so this is probably for an external stdlib replacement or overlay.

@SeedyROM
Copy link

SeedyROM commented Nov 4, 2021

Maybe I'm spoiled but the fact that this isn't available from the stdlib is a pretty weird design decision.

@nojb
Copy link
Contributor

nojb commented Nov 4, 2021

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/.

@xavierleroy
Copy link
Contributor

Since OCaml 4.07, you can go through sequences, e.g. List.of_seq (String.to_seq s). But if you feel the need for this kind of conversions, maybe you should work with character sequences (type char Seq.t) to begin with. Sequences can be much more space efficient than lists because sequences are evaluated on demand.

@SeedyROM
Copy link

SeedyROM commented Nov 4, 2021

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 List.of_seq (String.to_seq s) is what I was looking for though. Sorry for bothering all of you.

Also @nojb that old thread is out there, I'm super new to OCaml how did you even find that?

@nojb
Copy link
Contributor

nojb commented Nov 4, 2021

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 caml-list archives https://inbox.ocaml.org/caml-list, especially on the early years of this century.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants