* [Caml-list] Re: "ocaml_beginners"::[] string to list [not found] ` <20030217222143.GH84038@merkur.cert.siemens.de> @ 2003-02-17 22:53 ` Issac Trotts 2003-02-17 23:00 ` Michal Moskal ` (2 more replies) 0 siblings, 3 replies; 6+ messages in thread From: Issac Trotts @ 2003-02-17 22:53 UTC (permalink / raw) To: OCaml List 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'] Issac ------------------- 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 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Re: "ocaml_beginners"::[] string to list 2003-02-17 22:53 ` [Caml-list] Re: "ocaml_beginners"::[] string to list Issac Trotts @ 2003-02-17 23:00 ` Michal Moskal 2003-02-17 23:07 ` Brian Hurt [not found] ` <20030217225358.GI84038@merkur.cert.siemens.de> 2 siblings, 0 replies; 6+ messages in thread From: Michal Moskal @ 2003-02-17 23:00 UTC (permalink / raw) To: OCaml 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 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Re: "ocaml_beginners"::[] string to list 2003-02-17 22:53 ` [Caml-list] Re: "ocaml_beginners"::[] string to list Issac Trotts 2003-02-17 23:00 ` Michal Moskal @ 2003-02-17 23:07 ` Brian Hurt 2003-02-17 23:21 ` Issac Trotts [not found] ` <20030217225358.GI84038@merkur.cert.siemens.de> 2 siblings, 1 reply; 6+ messages in thread From: Brian Hurt @ 2003-02-17 23:07 UTC (permalink / raw) To: Issac Trotts; +Cc: OCaml List On Mon, 17 Feb 2003, 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'] > let explode s = let n = String.length s in let rec aux k accum = if k = n then (List.rev accum) else aux (k+1) (s.[k] :: accum) in aux 0 [] ;; See previous discussion. Brian ------------------- 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 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Re: "ocaml_beginners"::[] string to list 2003-02-17 23:07 ` Brian Hurt @ 2003-02-17 23:21 ` Issac Trotts 2003-02-18 8:19 ` sebastien FURIC 0 siblings, 1 reply; 6+ messages in thread From: Issac Trotts @ 2003-02-17 23:21 UTC (permalink / raw) To: OCaml List Brian Hurt wrote: >On Mon, 17 Feb 2003, 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'] >> >> >> > >let explode s = > let n = String.length s in > let rec aux k accum = if k = n then (List.rev accum) > else aux (k+1) (s.[k] :: accum) > in > aux 0 [] >;; > >See previous discussion. > Good point: we want it to be tail-recursive. Just to clean up, and make it more efficient... let explode s = let rec aux k accum = if k = -1 then accum else aux (k-1) (s.[k] :: accum) in aux (String.length s - 1) [] ;; Issac > >Brian > > > > > ------------------- 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 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Re: "ocaml_beginners"::[] string to list 2003-02-17 23:21 ` Issac Trotts @ 2003-02-18 8:19 ` sebastien FURIC 0 siblings, 0 replies; 6+ messages in thread From: sebastien FURIC @ 2003-02-18 8:19 UTC (permalink / raw) To: OCaml List Issac Trotts a écrit : > > Brian Hurt wrote: > > >On Mon, 17 Feb 2003, 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'] > >> > >> > >> > > > >let explode s = > > let n = String.length s in > > let rec aux k accum = if k = n then (List.rev accum) > > else aux (k+1) (s.[k] :: accum) > > in > > aux 0 [] > >;; > > > >See previous discussion. > > > Good point: we want it to be tail-recursive. > Just to clean up, and make it more efficient... > > let explode s = > let rec aux k accum = if k = -1 then accum > else aux (k-1) (s.[k] :: accum) > in > aux (String.length s - 1) [] > ;; Sometimes the imperative version seems more clear: let explode s = let res = ref [] in for i = String.length s - 1 downto 0 do res := s.[i] :: !res done; !res Sébastien. ------------------- 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 ^ permalink raw reply [flat|nested] 6+ messages in thread
[parent not found: <20030217225358.GI84038@merkur.cert.siemens.de>]
* Re: [Caml-list] Re: "ocaml_beginners"::[] string to list [not found] ` <20030217225358.GI84038@merkur.cert.siemens.de> @ 2003-02-17 23:11 ` Issac Trotts 0 siblings, 0 replies; 6+ messages in thread From: Issac Trotts @ 2003-02-17 23:11 UTC (permalink / raw) To: OCaml List Christian Schaller wrote: >Issac, > >Thanks a lot for this. I am somewhat surprised, since this should be a >built-in function. Anyway, maybe you can help me with this one, too: is >there also a conversion from character to string or appending a character in >front or at the end of a string? > >- Chris > Hi Chris, Here's a way to make a string from a char: # let c = 'a';; val c : char = 'a' # String.make 1 c;; - : string = "a" It's possible to prepend and append like this: # (String.make 1 c) ^ "foo";; - : string = "afoo" # "foo" ^ (String.make 1 c);; - : string = "fooa" but it is probably more efficient to use the Buffer module (buffer.mli in /usr/lib/ocaml or /usr/local/lib/ocaml). Issac ------------------- 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 ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2003-02-18 21:03 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- [not found] <auaq5u+94ll@eGroups.com> [not found] ` <20030217222143.GH84038@merkur.cert.siemens.de> 2003-02-17 22:53 ` [Caml-list] Re: "ocaml_beginners"::[] string to list Issac Trotts 2003-02-17 23:00 ` Michal Moskal 2003-02-17 23:07 ` Brian Hurt 2003-02-17 23:21 ` Issac Trotts 2003-02-18 8:19 ` sebastien FURIC [not found] ` <20030217225358.GI84038@merkur.cert.siemens.de> 2003-02-17 23:11 ` Issac Trotts
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox