Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Strings as arrays or lists...
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Eric C. Cooper <ecc@c...>
Subject: Re: [Caml-list] Strings as arrays or lists...
On Sun, Mar 02, 2003 at 07:34:37PM +0100, Xavier Leroy wrote:
> Actually, the list representation of strings is so repugnant that I
> don't even want to include "explode" and "implode" coercions between
> string and char list in the standard library.  A standard library
> should steer users away from algorithmically-inefficient code.  By not
> having implode and explode in the library, I hope OCaml programmers
> will come to the realization that the proper way to operate on strings
> is either via block operations (the String module, regexps, etc), or
> by recursion on integer indices.

I recently wrote some code that made use of char lists, and explode
and implode came in handy.  I needed to marshal a recursive datatype
into a packet to be sent over a communication channel according to a
protocol that imposed a specific format, including a length byte at
the beginning and a checksum byte at the end.

I could have made one pass over the data to compute the packet length,
then a second pass marshaling it into a buffer.  But it was very
natural to just build up a list of bytes in a single traversal of the
datatype.  Then the length and checksum could easily be added to the
beginning and the end, and the result written out.

I used explode when I encountered string values at the leaves of the
datatype.  I didn't really need implode (I could just iterate
output_byte over the final list), but it came in handy for dumping
packets for debugging.

I wasn't really using char lists as a representation of strings, but
rather as a buffer-like data structure that just happened to need
conversion to and from strings at certain points.

As another poster pointed out, explode and implode are analogous to
Array.to_list and Array.of_list, which don't seem to entice OCaml
programmers down the path of algorithmic ineffiency.

-- 
Eric C. Cooper          e c c @ c m u . e d u

-------------------
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