<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/12/bc9dc2b12a47d4bcfa85daeb30c1aa27"
  from="Jean-Baptiste Rouquier &lt;jean-baptiste.rouquier@e...&gt;"
  author="Jean-Baptiste Rouquier"
  date="2003-12-09T16:58:50"
  subject="Re: [Caml-list] stream conversion"
  prev="2003/12/534b54b6c437375ca8a5912a6be2fe6c"
  next="2003/12/bac10dac5f02b6339a9ca9189c75cddc"
  prev-in-thread="2003/12/8625cd8b15d41a5025c1435d3710629d"
  next-in-thread="2003/12/bac10dac5f02b6339a9ca9189c75cddc"
  prev-thread="2003/12/138d25e04135c86384c9d95218fd7b00"
  next-thread="2003/12/336da68464ef2651ac3a251d0d53f270"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] stream conversion">
<msg 
  url="2003/12/3ae47766c120e8d93ed0b4d9330f4667"
  from="Dustin Sallings &lt;dustin@s...&gt;"
  author="Dustin Sallings"
  date="2003-12-09T07:56:15"
  subject="[Caml-list] stream conversion">
<msg 
  url="2003/12/8625cd8b15d41a5025c1435d3710629d"
  from="Remi Vanicat &lt;remi.vanicat@l...&gt;"
  author="Remi Vanicat"
  date="2003-12-09T12:07:57"
  subject="Re: [Caml-list] stream conversion">
<msg 
  url="2003/12/bc9dc2b12a47d4bcfa85daeb30c1aa27"
  from="Jean-Baptiste Rouquier &lt;jean-baptiste.rouquier@e...&gt;"
  author="Jean-Baptiste Rouquier"
  date="2003-12-09T16:58:50"
  subject="Re: [Caml-list] stream conversion">
<msg 
  url="2003/12/bac10dac5f02b6339a9ca9189c75cddc"
  from="brogoff@s..."
  author="brogoff@s..."
  date="2003-12-09T17:25:29"
  subject="Re: [Caml-list] stream conversion">
<msg 
  url="2003/12/95d033f811e1ab445412e643be3da233"
  from="Jean-Baptiste Rouquier &lt;jean-baptiste.rouquier@e...&gt;"
  author="Jean-Baptiste Rouquier"
  date="2003-12-09T17:40:23"
  subject="Re: [Caml-list] stream conversion">
</msg>
<msg 
  url="2003/12/5c54ecb9bfb3b517c0719bf7c74d646a"
  from="Tim Freeman &lt;tim@f...&gt;"
  author="Tim Freeman"
  date="2003-12-09T19:43:51"
  subject="Re: [Caml-list] stream conversion">
<msg 
  url="2003/12/7705bdf1a3ecb740b46d00b434db07f4"
  from="Damien Doligez &lt;damien.doligez@i...&gt;"
  author="Damien Doligez"
  date="2003-12-16T22:41:15"
  subject="Re: [Caml-list] stream conversion">
</msg>
</msg>
</msg>
<msg 
  url="2003/12/9947980da386f0a0a076c97377241cae"
  from="Dustin Sallings &lt;dustin@s...&gt;"
  author="Dustin Sallings"
  date="2003-12-09T19:13:29"
  subject="Re: [Caml-list] stream conversion">
</msg>
</msg>
<msg 
  url="2003/12/6ca04e3679ec1004184b76bd53774151"
  from="Dustin Sallings &lt;dustin@s...&gt;"
  author="Dustin Sallings"
  date="2003-12-09T19:15:21"
  subject="Re: [Caml-list] stream conversion">
</msg>
</msg>
<msg 
  url="2003/12/9ee9052e21abd29b23b1757d11f1890b"
  from="Nicolas Cannasse &lt;warplayer@f...&gt;"
  author="Nicolas Cannasse"
  date="2003-12-10T01:09:52"
  subject="Re: [Caml-list] stream conversion">
</msg>
</msg>
</thread>

<contents>
Dustin Sallings &lt;dustin@spy.net&gt; wrote:

&gt;I have a need to convert a ``string stream'' to a ``char stream.''
&gt;
Small is beautiful !
#load "camlp4o.cma";;
let rec convert = parser
  | [&lt; 'string; stream &gt;] -&gt; [&lt;Stream.of_string string; convert stream&gt;]
  | [&lt;&gt;] -&gt; [&lt;&gt;]

(* That's it... However this function compiles but doesn't work.*)
(* Can anybody explain the following problem: *)
let foo = [&lt; Stream.of_string "bar"; Stream.of_string "quz"&gt;]
let _ = Stream.next foo
(* Exception: Failure "illegal stream concatenation". *)




(* So I have to implement my own (awful : skip it!) concatenation 
function : *)
let concat s s' =
  let stream_next =
    let rec f = ref (fun _ -&gt;
      try Some (Stream.next s)
      with Stream.Failure -&gt;
    Printf.printf "redefining f\n%!";
    f := (fun _ -&gt;
        (try Some (Stream.next s')
         with Stream.Failure -&gt; None));
    !f 0) in
    fun x -&gt; !f x
  in Stream.from stream_next


(* And then slightly rewrite my function : *)

let rec convert = parser
  | [&lt; 'string; stream &gt;] -&gt; concat (Stream.of_string string) (convert 
stream)
  | [&lt;&gt;] -&gt; [&lt;&gt;]



(*let's test it*)
let foo = convert (Stream.of_list ["foo"; "bar"; ""; "qux"])

let _ = Stream.next foo;;
  (* - : char = 'f' *)
let _ = Stream.next foo;;
  (* - : char = 'o' *)
let _ = Stream.next foo;;
  (* - : char = 'o' *)
let _ = Stream.next foo;;
  (* redefining f *)
  (* - : char = 'b' *)
let _ = Stream.next foo;;
  (* - : char = 'a' *)
let _ = Stream.next foo;;
  (* - : char = 'r' *)
let _ = Stream.next foo;;
  (* redefining f *)
  (* redefining f *)
  (* - : char = 'q' *)
let _ = Stream.next foo;;
  (* - : char = 'u' *)
let _ = Stream.next foo;;
  (* - : char = 'x' *)
let _ = Stream.next foo;;
  (* redefining f *)
  (* Exception: Stream.Failure. *)


Except the concatenation problem, I think camlp4 makes it much clearer, 
and it works with empty strings.

Jean-Baptiste Rouquier.

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

</contents>

</message>

