[Haskell] Simulating client server communication with recursive
monads
Jan Stranik
janstranik at yahoo.de
Tue Jan 15 13:19:07 EST 2008
John,
Your change makes just the server function to return the same number that it
receives. My server implementation multiplied the incoming number by two.
My question was why does the output from writer shows first all output from
server, then followed by all output from client. I would like to see output
from client and server to alternate in the order in which the computation
occurs, namely [server, client, server, client, server, client, .].
I know that for finite sequences, it is easily possible to reorder the
output from writer. However, it is not possible to reorder the output if the
sequence is infinite. That can be seen in my example program if it is
modified by removing the statement "take 10". In that case the program never
prints output from clients, the output printer will be only from the server.
Jan
From: John Vogel [mailto:jpvogel1 at gmail.com]
Sent: Monday, January 14, 2008 8:33 PM
To: Jan Stranik
Cc: haskell at haskell.org
Subject: Re: [Haskell] Simulating client server communication with recursive
monads
If you redefine as follows:
server :: [Integer] -> Writer [String] [Integer]
server [] = return []
server (a:as) = do
tell ["Server " ++ show a]
rs <- server as
return (a:rs)
You get this for the output:
["Server 0","Server 1","Server 2","Server 3","Server 4","Server 5","Server
6","Server 7","Server 8","Server 9","Server 1
0","Client 0","Client 1","Client 2","Client 3","Client 4","Client 5","Client
6","Client 7","Client 8","Client 9"]
Then you just need to alternate the pattern. Though a real simulation of
sever traffic would account for the packets
not being recieved, rerequested, or recieved out of order. It also depends
whether you are simulating TCP or UDP.
On 1/14/08, Jan Stranik <janstranik at yahoo.de> wrote:
Hello,
I am trying to simulate a client server traffic using recursive lazy
evaluation. I am trying to do that in a recursive writer monad.
Following code is my attempt to simulate client server interaction and
collect its transcript:
{-# OPTIONS -fglasgow-exts #-}
module Main where
import Control.Monad.Writer.Lazy
simulation:: Writer [String] ()
simulation = mdo
a <- server cr
cr <- client $ take 10 a
return ()
server:: [Integer] -> Writer [String] [Integer]
server (a:as) = do
tell ["server " ++ show a]
rs <- server as
return ((a*2):rs)
server [] = return []
client:: [Integer] -> Writer [String] [Integer]
client as = do
dc <- doClient as
return (0:dc)
where
doClient (a:as) = do
tell ["Client " ++ show a]
as' <- doClient as
return ((a+1):as')
doClient [] = return []
main = return $ snd $ runWriter simulation
The problem that I see is that the transcript collected contains first all
output from the server, and then output from the client.
Here is an example of output that I see:
:["server 0","server 1","server 3","server 7","server 15","server
31","server 63","server 127","server 255","server 511","server 1023","Client
0","Client 2","Client 6","Client 14","Client 30","Client 62","Client
126","Client 254","Client 510","Client 1022"]
I would like to collect the output like:
:["client 0","server 0", "client 1",.]
This would allow me to remove the ending condition in simulation (take 10),
and instead rely fully on lazy evaluation to collect as many simulation
steps as needed by my computation.
I am still relatively new to the concepts of recursive monadic computations,
so I would appreciate any suggestions from experts on this mailing list.
Thank you
Jan
_______________________________________________
Haskell mailing list
Haskell at haskell.org
http://www.haskell.org/mailman/listinfo/haskell
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell/attachments/20080115/92e45919/attachment-0001.htm
More information about the Haskell
mailing list