List Info

Thread: "ocaml_beginners"::[] string + char




"ocaml_beginners"::[] string + char
user name
2006-08-15 16:58:47
Is there a standard function which will append a character
to a
string? Failing that, is there a better way to do it than

let addchar str c =
  let len = String.length str in
  let retval = String.create (len + 1) in
  String.blit str 0 retval 0 len;
  retval.[len] <- c;
  retval

In general, OCaml's support for working with strings as
sequences of
characters seems rather weak - is there a library that
addresses this?

martin


Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 



"ocaml_beginners"::[] string + char
user name
2006-08-15 17:11:42
Martin DeMello wrote:
> Is there a standard function which will append a
character to a
> string? Failing that, is there a better way to do it
than
> 
> let addchar str c =
> let len = String.length str in
> let retval = String.create (len + 1) in
> String.blit str 0 retval 0 len;
> retval.[len] <- c;
> retval
> 
> In general, OCaml's support for working with strings
as sequences of
> characters seems rather weak - is there a library that
addresses this?

Check out module Buffer in the standard library.

Robert Roessler
robertrrftp.com
http://www.rftp.com


Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 


"ocaml_beginners"::[] string + char
user name
2006-08-15 17:09:11
On Tue, Aug 15, 2006 at 10:28:47PM +0530, Martin DeMello
wrote:
> Is there a standard function which will append a
character to a
> string? Failing that, is there a better way to do it
than
> 
> let addchar str c =
>   let len = String.length str in
>   let retval = String.create (len + 1) in
>   String.blit str 0 retval 0 len;
>   retval.[len] <- c;
>   retval
> 
> In general, OCaml's support for working with strings
as sequences of
> characters seems rather weak - is there a library that
addresses this?

Look no further than the 'Buffer' module, which does
exactly
what you need.

Rich.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com


Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 



"ocaml_beginners"::[] string + char
user name
2006-08-15 17:17:06
On Tue, 15 Aug 2006, Martin DeMello wrote:

> Is there a standard function which will append a
character to a
> string? Failing that, is there a better way to do it
than
>
> let addchar str c =
     ...

I suppose that depends on how often you'll be appending to
the string.  If 
you're doing it a lot, you can either make use of the
Buffer module or 
write a new String module that will extend the length of the
string by 
more than one slot att a time, and monitor the current
actual length of 
the string.

> In general, OCaml's support for working with strings
as sequences of
> characters seems rather weak - is there a library that
addresses this?

Well, not really (depending on how you're planning on
working with 
strings).  There is the Buffer module if you want to work
with extensible 
strings, but all it's really good for is extending strings
and nothing 
else.  There's also extlib, which doesn't have what you
want, but does add 
some useful functions.

William D. Neumann

---

"There's just so many extra children, we could just
feed the
children to these tigers.  We don't need them, we're not
doing 
anything with them.

Tigers are noble and sleek; children are loud and
messy."

         -- Neko Case

Life is unfair.  Kill yourself or get over it.
 	-- Black Box Recorder


Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 



"ocaml_beginners"::[] string + char
user name
2006-08-15 19:14:02
On 8/15/06, William D. Neumann <wneumanncs.unm.edu> wrote:
>
>  I suppose that depends on how often you'll be
appending to the string.  If
>  you're doing it a lot, you can either make use of the
Buffer module or
>  write a new String module that will extend the length
of the string by
>  more than one slot att a time, and monitor the current
actual length of
>  the string.

Thanks - the Buffer looks like what I need (and thanks to
everyone
else who suggested it too).

>  > In general, OCaml's support for working with
strings as sequences of
>  > characters seems rather weak - is there a library
that addresses this?
>
>  Well, not really (depending on how you're planning on
working with
>  strings).  There is the Buffer module if you want to
work with extensible
>  strings, but all it's really good for is extending
strings and nothing
>  else.  There's also extlib, which doesn't have what
you want, but does add
>  some useful functions.

I'm matching a pattern, "path", against a trie
as follows:

let rec build ix str path =
  let next i p = build (ptr i) (addchar str (letter i)) p in
  match path with
  | [c] -> begin
    match c with
    | '.' -> allwords ix str
    | _   -> if (wordp (find ix c)) then [(addchar str
c)] else []
  end
  | c :: cs -> begin
    if (ptr ix) = 0 then []
    else
      match c with
      | '.' -> List.flatten (mapsibs (fun i -> next
i cs) ix)
      | _   -> next (find ix c) cs
  end

"str" is the prefix that gets passed into the
subtrie at each stage,
and the recursive call adds the character contained in the
current
node to the end of str and traverses to the next edge. So
I'll have to
copy str anyway when mapping over a node's siblings, since
each
sibling will add something different to the prefix - is a
Buffer still
efficient when it needs to be cloned a lot or should I stick
to a
plain String in this case?

martin


Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 



"ocaml_beginners"::[] string + char
user name
2006-08-15 20:04:38
On Wed, 16 Aug 2006, Martin DeMello wrote:

> On 8/15/06, William D. Neumann <wneumanncs.unm.edu> wrote:
> >
> >  I suppose that depends on how often you'll be
appending to the string.  If
> >  you're doing it a lot, you can either make use
of the Buffer module or
> >  write a new String module that will extend the
length of the string by
> >  more than one slot att a time, and monitor the
current actual length of
> >  the string.
>
> Thanks - the Buffer looks like what I need (and thanks
to everyone
> else who suggested it too).
>
> >  > In general, OCaml's support for working
with strings as sequences of
> >  > characters seems rather weak - is there a
library that addresses this?
> >
> >  Well, not really (depending on how you're
planning on working with
> >  strings).  There is the Buffer module if you want
to work with extensible
> >  strings, but all it's really good for is
extending strings and nothing
> >  else.  There's also extlib, which doesn't have
what you want, but does add
> >  some useful functions.
>
> I'm matching a pattern, "path", against a
trie as follows:
>
> let rec build ix str path =
>   let next i p = build (ptr i) (addchar str (letter i))
p in
>   match path with
>   | [c] -> begin
>     match c with
>     | '.' -> allwords ix str
>     | _   -> if (wordp (find ix c)) then [(addchar
str c)] else []
>   end
>   | c :: cs -> begin
>     if (ptr ix) = 0 then []
>     else
>       match c with
>       | '.' -> List.flatten (mapsibs (fun i ->
next i cs) ix)
>       | _   -> next (find ix c) cs
>   end
>
> "str" is the prefix that gets passed into
the subtrie at each stage,
> and the recursive call adds the character contained in
the current
> node to the end of str and traverses to the next edge.
So I'll have to
> copy str anyway when mapping over a node's siblings,
since each
> sibling will add something different to the prefix - is
a Buffer still
> efficient when it needs to be cloned a lot or should I
stick to a
> plain String in this case?

Probably you should use reversed lists instead:

  let addchar prefix next = next :: prefix


Martin

--
Martin Jambon, PhD
http://martin.jambon.fre
e.fr


Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 



"ocaml_beginners"::[] string + char
user name
2006-08-15 20:19:28
On Wed, 16 Aug 2006, Martin DeMello wrote:

> I'm matching a pattern, "path", against a
trie as follows:
>
> let rec build ix str path =
>  let next i p = build (ptr i) (addchar str (letter i))
p in
>  match path with
>  | [c] -> begin
>    match c with
>    | '.' -> allwords ix str
>    | _   -> if (wordp (find ix c)) then [(addchar
str c)] else []
>  end
>  | c :: cs -> begin
>    if (ptr ix) = 0 then []
>    else
>      match c with
>      | '.' -> List.flatten (mapsibs (fun i ->
next i cs) ix)
>      | _   -> next (find ix c) cs
>  end
>
> "str" is the prefix that gets passed into
the subtrie at each stage,
> and the recursive call adds the character contained in
the current
> node to the end of str and traverses to the next edge.
So I'll have to
> copy str anyway when mapping over a node's siblings,
since each
> sibling will add something different to the prefix - is
a Buffer still
> efficient when it needs to be cloned a lot or should I
stick to a
> plain String in this case?

Well, if you look at the code in buffer.ml, you'll see that
they're doing 
essentially what I suggested as an option, resizing a string
and keeping 
track of the current position in the string, so pulling out
the contents 
is essentially a call to String.sub, and cloning is
essentially a call to 
String.create followed by String.blit (plus an extra
indirection for 
record access).

However, there is a bit of an issue here, and that's the
mutable nature of 
the buffer, and you're likely to run up against it in your
call to mapsibs 
if you're not careful.  E.g.
# let a = Array.init 10 (fun i -> 97 + i);;
let b = Buffer.create 20;;
Buffer.add_string b "Test: ";;
let f () =
   let next i = Buffer.add_char b (char_of_int a.(i));
Buffer.contents b in
   List.map (fun i -> next i) [0;1;2;3;4;5;6;7;8;9]
;;
val a : int array = [|97; 98; 99; 100; 101; 102; 103; 104;
105; 106|]
# val b : Buffer.t = <abstr>
# - : unit = ()
#       val f : unit -> string list = <fun>
# Buffer.contents b;;
- : string = "Test: "
# f ();;
- : string list =
["Test: a"; "Test: ab"; "Test:
abc"; "Test: abcd"; "Test:
abcde";
  "Test: abcdef"; "Test: abcdefg";
"Test: abcdefgh"; "Test: abcdefghi";
  "Test: abcdefghij"]
# Buffer.contents b;;
- : string = "Test: abcdefghij"

You see here that the buffer just kept getting added to,
rather than 
getting mapped to a new string with a different last
character in the last 
position (if that's what you want).  This is oposed to:
# let a = Array.init 10 (fun i -> 97 + i);;
let f str =
   let next i = addchar str (char_of_int a.(i)) in
   List.map (fun i -> next i) [0;1;2;3;4;5;6;7;8;9]
;;
val a : int array = [|97; 98; 99; 100; 101; 102; 103; 104;
105; 106|]
#       val f : string -> string list = <fun>
# f "Test: ";;
- : string list =
["Test: a"; "Test: b"; "Test:
c"; "Test: d"; "Test: e";
"Test: f"; "Test: 
g"; "Test: h"; "Test: i";
"Test: j"]

William D. Neumann

---

"There's just so many extra children, we could just
feed the
children to these tigers.  We don't need them, we're not
doing 
anything with them.

Tigers are noble and sleek; children are loud and
messy."

         -- Neko Case

Life is unfair.  Kill yourself or get over it.
 	-- Black Box Recorder


Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 



"ocaml_beginners"::[] string + char
user name
2006-08-16 14:08:12
On 8/16/06, Martin Jambon <martin_jambonemailuser.net> wrote:
>
>  Probably you should use reversed lists instead:
>
>  let addchar prefix next = next :: prefix

Nice idea! I was quite disappointed that
'c'::"str" didn't work,
though  Was hoping
to avoid the implode/explode steps.

martin


Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 


"ocaml_beginners"::[] string + char
user name
2006-08-16 14:13:28
On 8/16/06, William D. Neumann <wneumanncs.unm.edu> wrote:
>
>  However, there is a bit of an issue here, and that's
the mutable nature of
>  the buffer, and you're likely to run up against it in
your call to mapsibs
>  if you're not careful.  E.g.

[...]

Yeah, it looks like a buffer is not what I need, since I
don't
actually want to mutate a string once it's returned. Martin
Jambon's
idea of reversed lists seems like it'll do the trick - I
take it the
compiler is smart enough to store a list of chars as a
string (i.e.
without space overhead)?

martin


Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 


"ocaml_beginners"::[] string + char
user name
2006-08-16 14:46:22
On Wed, Aug 16, 2006 at 07:43:28PM +0530, Martin DeMello
wrote:
> Yeah, it looks like a buffer is not what I need, since
I don't
> actually want to mutate a string once it's returned.
Martin Jambon's
> idea of reversed lists seems like it'll do the trick -
I take it the
> compiler is smart enough to store a list of chars as a
string (i.e.
> without space overhead)?

Definitely not!  char list <> string.

Rich.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com


Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 


[1-10] [11-16]

about | contact  Other archives ( Real Estate discussion Medical topics )