<HTML>


<FONT FACE="MS Shell Dlg" DEFAULT="FACE"><FONT SIZE="1" POINTSIZE="8" DEFAULT="SIZE">Hello Magnus,<BR>

<BR>

You are right. Silly me. I was thinking of Rel as a kind of Hashtable. In this case, <BR>

I think it should be possible to have O(n), since "cons" would only need constant <BR>

time.<BR>

<BR>

Cheers,<BR>

<BR>

Bruno<BR>

<BR>

On Thu, 14 Sep 2006 13:11:05 -0400 (EDT), Magnus Jonsson wrote:<BR>

<BR>

&gt;Thanks Bruno.<BR>

&gt;<BR>

&gt;However I think this is still O(n*m). As far as I understand your code it <BR>

&gt;is a longwinded way to say "[b | (a,b) &lt;- input, b == <BR>

&gt;myChannelOfInterest]". This is fine if you are only interested in one <BR>

&gt;channel, and for that case I agree it's O(n), but if you are interested in <BR>

&gt;many different channels, it will take O(n*m). Here's why I think your <BR>

&gt;code is equivalent to using a filter/list-comprehension:<BR>

&gt;<BR>

&gt;&gt;&gt; cons :: Eq a =&gt; (a,b) -&gt; Rel a b -&gt; Rel a b<BR>

&gt;&gt;&gt; cons (x,y) l z<BR>

&gt;&gt;&gt;    | x == z        = y : l x<BR>

&gt;&gt;&gt;    | otherwise  = l z<BR>

&gt;<BR>

&gt;z is the channel that the user is interested in.<BR>

&gt;x is the channel of the current message in the list.<BR>

&gt;y is the message content.<BR>

&gt;l is a function for searching the rest of the list.<BR>

&gt;<BR>

&gt;For each element of the list you create a function that compares the <BR>

&gt;requested channel to a (in (a,b)). If it's the same, you return that <BR>

&gt;element followed by a continued search down the list. If you don't find <BR>

&gt;it, you forward the request to the next function down the list. That's <BR>

&gt;exactly the same as what the filter function does.<BR>

&gt;<BR>

&gt;It is possible I made a mistake somewhere and if I did, let me know.<BR>

&gt;<BR>

&gt;/ Magnus<BR>

&gt;<BR>

&gt;On Thu, 14 Sep 2006, Bruno Oliveira wrote:<BR>

&gt;<BR>

&gt;&gt; Hello,<BR>

&gt;&gt;<BR>

&gt;&gt; I think it is possible to do it in O(n) (if you change the representation<BR>

&gt;&gt; of the output stream).<BR>

&gt;&gt;<BR>

&gt;&gt; Note that the solution is quite similar toTwan van Laarhoven,<BR>

&gt;&gt; and it should be trivial use "Map" instead of "Rel".<BR>

&gt;&gt;<BR>

&gt;&gt; Here is my take on it:<BR>

&gt;&gt;<BR>

&gt;&gt; The type of the output stream:<BR>

&gt;&gt;<BR>

&gt;&gt;&gt; type Rel a b = a -&gt; [b]<BR>

&gt;&gt;<BR>

&gt;&gt; Here are cons and nil:<BR>

&gt;&gt;<BR>

&gt;&gt;&gt; nil :: Rel a b<BR>

&gt;&gt;&gt; nil _ = []<BR>

&gt;&gt;<BR>

&gt;&gt; and a lookUp function:<BR>

&gt;&gt;<BR>

&gt;&gt;&gt; lookUp :: Rel a b -&gt; a -&gt; [b]<BR>

&gt;&gt;&gt; lookUp = id<BR>

&gt;&gt;<BR>

&gt;&gt; Finally the splitStreams algorithm:<BR>

&gt;&gt;<BR>

&gt;&gt;&gt; splitStreams :: Eq a =&gt; [(a,b)] -&gt; Rel a b<BR>

&gt;&gt;&gt; splitStreams = foldr cons nil<BR>

&gt;&gt;<BR>

&gt;&gt; Here is a test with finite lists:<BR>

&gt;&gt;<BR>

&gt;&gt;&gt; fin = splitStreams [(3,'x'),(1,'y'),(3,'z'),(2,'w')]<BR>

&gt;&gt;<BR>

&gt;&gt; and here are the console tests:<BR>

&gt;&gt;<BR>

&gt;&gt; *General7&gt; lookUp fin 1<BR>

&gt;&gt; "y"<BR>

&gt;&gt; *General7&gt; lookUp fin 2<BR>

&gt;&gt; "w"<BR>

&gt;&gt; *General7&gt; lookUp fin 3<BR>

&gt;&gt; "xz"<BR>

&gt;&gt;<BR>

&gt;&gt; Now with infinite lists:<BR>

&gt;&gt;<BR>

&gt;&gt;&gt; inf = splitStreams (map (\x -&gt; (0, x)) [1..])<BR>

&gt;&gt;<BR>

&gt;&gt; and here a case where it terminates with infinite lists:<BR>

&gt;&gt;<BR>

&gt;&gt; *General7&gt; take 10 (lookUp inf 0)<BR>

&gt;&gt; [1,2,3,4,5,6,7,8,9,10]<BR>

&gt;&gt;<BR>

&gt;&gt; Cheers,<BR>

&gt;&gt;<BR>

&gt;&gt; Bruno Oliveira<BR>

&gt;&gt;<BR>

&gt;&gt;<BR>

&gt;&gt; _______________________________________________<BR>

&gt;&gt; Haskell-Cafe mailing list<BR>

&gt;&gt; <FONT COLOR=0000ff><U>Haskell-Cafe@haskell.org<FONT COLOR=000000 DEFAULT="COLOR"></U><BR>

&gt;&gt; <FONT COLOR=0000ff><U>http://www.haskell.org/mailman/listinfo/haskell-cafe<FONT COLOR=000000 DEFAULT="COLOR"></U><BR>

&gt;&gt;<BR>

<BR>


</HTML>