Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0005367OCamlOCaml generalpublic2011-10-02 20:062011-12-21 14:50
Reportermcandre 
Assigned To 
PrioritynormalSeverityfeatureReproducibilityN/A
StatusfeedbackResolutionreopened 
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
(0006152)
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.
  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.
(0006153)
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.

- 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


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker