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 · 3 comments
Closed

OCaml lacks conversion between string and char list. #5367

vicuna opened this issue Oct 2, 2011 · 3 comments

Comments

@vicuna
Copy link

@vicuna 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 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 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 vicuna commented Dec 7, 2016

Comment author: @mshinwell

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

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

Successfully merging a pull request may close this issue.

None yet
1 participant