Browse thread
[Caml-list] Re: "ocaml_beginners"::[] string to list
-
Issac Trotts
- Brian Hurt
- Issac Trotts
- Michal Moskal
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Michal Moskal <malekith@p...> |
| Subject: | Re: [Caml-list] Re: "ocaml_beginners"::[] string to list |
On Mon, Feb 17, 2003 at 02:53:07PM -0800, Issac Trotts wrote:
> Christian Schaller wrote:
>
> >Hi everyone!
> >
> >I'm trying to find a built in function for converting a string to a list of
> >characters, similar to SML's explode. Is something like this available?
> >
> >Thank you!
> >
> >- Chris
> >
> # let explode s =
> let n = String.length s in
> let rec aux k = if k = n then [] else s.[k] :: aux (k+1) in
> aux 0;;
> val explode : string -> char list = <fun>
> # explode "foo";;
> - : char list = ['f'; 'o'; 'o']
Not tail recursive :)
>From my patch to pleac (http://pleac.sourceforge.net):
(* convert string to list of chars *)
let explode s =
let rec f acc = function
| -1 -> acc
| k -> f (s.[k] :: acc) (k - 1)
in f [] (String.length s - 1)
(* convert list of chars to string *)
let implode l =
let s = String.create (List.length l) in
let rec f n = function
| x :: xs -> s.[n] <- x; f (n + 1) xs
| [] -> s
in f 0 l
PS: I know, it doesn't matter much if it's tail recursive or not, nobody
is going to use for data of size when it matters :-)
--
: Michal Moskal ::::: malekith/at/pld-linux.org : GCS {C,UL}++++$ a? !tv
: PLD Linux ::::::: Wroclaw University, CS Dept : {E-,w}-- {b++,e}>+++ h
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners