Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0005367OCaml~DO NOT USE (was: OCaml general)public2011-10-02 20:062016-12-07 14:54
Assigned To 
StatusclosedResolutionwon't fix 
PlatformOSOS Version
Product Version3.12.0 
Target VersionFixed in Version 
Summary0005367: OCaml lacks conversion between string and char list.
DescriptionCorrect 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?
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
doligez (administrator)
2011-10-05 19:23

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. (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.
mcandre (reporter)
2011-10-06 01:19

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.
shinwell (developer)
2016-12-07 14:54

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

- Issue History
Date Modified Username Field Change
2011-10-02 20:06 mcandre New Issue
2011-10-05 19:23 doligez Note Added: 0006152
2011-10-05 19:23 doligez Status new => closed
2011-10-05 19:23 doligez Resolution open => won't fix
2011-10-06 01:19 mcandre Note Added: 0006153
2011-10-06 01:19 mcandre Status closed => feedback
2011-10-06 01:19 mcandre Resolution won't fix => reopened
2016-12-07 14:54 shinwell Note Added: 0016716
2016-12-07 14:54 shinwell Status feedback => closed
2016-12-07 14:54 shinwell Resolution reopened => won't fix
2017-02-23 16:36 doligez Category OCaml general => -OCaml general
2017-03-03 17:55 doligez Category -OCaml general => -(deprecated) general
2017-03-03 18:01 doligez Category -(deprecated) general => ~deprecated (was: OCaml general)
2017-03-06 17:04 doligez Category ~deprecated (was: OCaml general) => ~DO NOT USE (was: OCaml general)

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker