Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Question on Lazy IO (Michael Snoyman)
2. Re: Type error in sub-function (Kim-Ee Yeoh)
3. Re: Type error in sub-function (Chul-Woong Yang)
4. Re: Question on Lazy IO (Chul-Woong Yang)
5. Re: Question on Lazy IO (Chul-Woong Yang)
----------------------------------------------------------------------
Message: 1
Date: Thu, 24 Jul 2014 10:54:33 +0300
From: Michael Snoyman <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Question on Lazy IO
Message-ID:
<CAKA2JgKuCZfmg0b8=bct+cqmxun+mr5er5cdrc7zcuhy3dg...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
My biggest recommendation would be not to use lazy I/O. But excluding
that...
You just need to make sure that as much of the input is actually received
before sending a response. You do that by forcing evaluation of the thunk.
With lazy I/O, forcing evaluation ends up causing I/O to be performed,
which is what you want in this case. As an example, to make sure that at
least one character is received before continuing, you could use this
function:
getSomeContents = do
x <- getContents
case x of
[] -> return x
_:_ -> return x
Another possibility is to use *some* strict I/O. For example, you can get
the first character strictly and the rest lazily:
getSomeContents = do
c <- getChar
x <- getContents
return $ c : x
Something else you should think about is what data representation you want
to use. Everything we've done here using String, which is (1) inefficient,
since it's an unpacked representation, and (2) incorrect, since HTTP data
is really a series of bytes, not a series of unicode code points. But I'm
going on the assumption that this exercise is more to understand I/O
evaluation.
On Thu, Jul 24, 2014 at 9:07 AM, ??? <[email protected]> wrote:
> Hi, all.
>
> Haskell's lazy IO causes below program to
> write back "ECHO result" to the user
> even if he doesn't give any input to the program.
>
> test1 :: IO ()
> test1 = do
> l <- fmap lines getContents
> putStrLn "ECHO result"
> putStr $ unlines l
>
> If the client logic, that is the part for feeding data to this program
> and wait for the response, expect the server logic to respond
> only after all the client's request data are feed to the server logic,
> the behaviour could be problematic.
>
> I'm now writing simple web server, as you might guess, and
> my web server responds "HTTP/1.1" before any client's request
> are given. I'm using hGetContents and web server's logic is
> basically same with above code skeleton.
>
> How can I tell the program to respond data only after all the
> requests are feed? Any directions are welcomed.
> Thanks.
>
> Chul-Woong
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140724/021273f3/attachment-0001.html>
------------------------------
Message: 2
Date: Thu, 24 Jul 2014 14:56:05 +0700
From: Kim-Ee Yeoh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Type error in sub-function
Message-ID:
<CAPY+ZdQ-E0P5L=hpztafm9zmussaelqaahzfnbrykjv6nfr...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Wed, Jul 23, 2014 at 9:45 PM, ??? <[email protected]> wrote:
> I changed the code like belows and it works.
On a higher-level, you might want to reconsider the design of that function
called parseHeader.
It has type: (a -> IO b) only because there's a special case "now" which
apparently requires querying for the current time. Other than that, it's a
pure function.
Is there a way to make it a pure function?
-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140724/6f4f5592/attachment-0001.html>
------------------------------
Message: 3
Date: Thu, 24 Jul 2014 17:53:30 +0900
From: Chul-Woong Yang <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Type error in sub-function
Message-ID:
<CALmycjraYj6X4ptfRibFPVq9b=qvo8edhbhdzcol0wdby_e...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Yes. I found that issue and change the function to pure form which receives
time as input argument.
When I tried to code in C++, I found myself coding in C only to feed the
sources to g++.
I found myself coding in do-block for majority of Haskell code now. :-(
Thanks Yeoh!
2014-07-24 16:56 GMT+09:00 Kim-Ee Yeoh <[email protected]>:
>
> On Wed, Jul 23, 2014 at 9:45 PM, ??? <[email protected]> wrote:
>
>> I changed the code like belows and it works.
>
>
> On a higher-level, you might want to reconsider the design of that
> function called parseHeader.
>
> It has type: (a -> IO b) only because there's a special case "now" which
> apparently requires querying for the current time. Other than that, it's a
> pure function.
>
> Is there a way to make it a pure function?
>
> -- Kim-Ee
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140724/622e8895/attachment-0001.html>
------------------------------
Message: 4
Date: Thu, 24 Jul 2014 18:03:13 +0900
From: Chul-Woong Yang <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Question on Lazy IO
Message-ID:
<calmycjrosfleca3e4xomx1ak6c1ex3lospkmtbkmjxunw6h...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Wow. DeepSeq is cool!
I 'deepseq'ed received request header and everything is OK.
Thanks Santtu!
2014-07-24 16:53 GMT+09:00 Santtu Keskinen <[email protected]>:
> Hi Chul-Woong,
>
> Deepseq package can be used to make sure a list is actually fully
> constructed before it's used.
>
> import System.IO
> import Control.DeepSeq
>
> main :: IO ()
> main = do
> s <- getContents
> s `deepseq` hClose stdin
> putStrLn "ECHO result"
> putStr s
>
> Cheers
>
>
> 2014-07-24 9:07 GMT+03:00 ??? <[email protected]>:
>
>> Hi, all.
>>
>> Haskell's lazy IO causes below program to
>> write back "ECHO result" to the user
>> even if he doesn't give any input to the program.
>>
>> test1 :: IO ()
>> test1 = do
>> l <- fmap lines getContents
>> putStrLn "ECHO result"
>> putStr $ unlines l
>>
>> If the client logic, that is the part for feeding data to this program
>> and wait for the response, expect the server logic to respond
>> only after all the client's request data are feed to the server logic,
>> the behaviour could be problematic.
>>
>> I'm now writing simple web server, as you might guess, and
>> my web server responds "HTTP/1.1" before any client's request
>> are given. I'm using hGetContents and web server's logic is
>> basically same with above code skeleton.
>>
>> How can I tell the program to respond data only after all the
>> requests are feed? Any directions are welcomed.
>> Thanks.
>>
>> Chul-Woong
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140724/1d2b6a11/attachment-0001.html>
------------------------------
Message: 5
Date: Thu, 24 Jul 2014 18:08:50 +0900
From: Chul-Woong Yang <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Question on Lazy IO
Message-ID:
<calmycjo8b621sogh3hxxqqanzxwrmea1wu1xruqqj6g3yfc...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Thank you, Michael, for kind explanation.
My first approach was calling the following sentinel function
before proceeding:
isValidReq req = (type req) /= ERROR && (length $ hdrs req) < 10000
The hdrs of (web) request are constructed lazily, so I should do length
call to
wait until full request are read.
With HTTP processing, I cannot figure out how to do with strict I/O.
Maybe complex parsing (parsec?) is required, I think.
So I changed the sentinel function using deepseq.
isVaidReq req = (hdrs req) `deepseq` (type req) /= ERROR
Regards,
Chul-Woong
2014-07-24 16:54 GMT+09:00 Michael Snoyman <[email protected]>:
> My biggest recommendation would be not to use lazy I/O. But excluding
> that...
>
> You just need to make sure that as much of the input is actually received
> before sending a response. You do that by forcing evaluation of the thunk.
> With lazy I/O, forcing evaluation ends up causing I/O to be performed,
> which is what you want in this case. As an example, to make sure that at
> least one character is received before continuing, you could use this
> function:
>
> getSomeContents = do
> x <- getContents
> case x of
> [] -> return x
> _:_ -> return x
>
> Another possibility is to use *some* strict I/O. For example, you can get
> the first character strictly and the rest lazily:
>
> getSomeContents = do
> c <- getChar
> x <- getContents
> return $ c : x
>
> Something else you should think about is what data representation you want
> to use. Everything we've done here using String, which is (1) inefficient,
> since it's an unpacked representation, and (2) incorrect, since HTTP data
> is really a series of bytes, not a series of unicode code points. But I'm
> going on the assumption that this exercise is more to understand I/O
> evaluation.
>
>
> On Thu, Jul 24, 2014 at 9:07 AM, ??? <[email protected]> wrote:
>
>> Hi, all.
>>
>> Haskell's lazy IO causes below program to
>> write back "ECHO result" to the user
>> even if he doesn't give any input to the program.
>>
>> test1 :: IO ()
>> test1 = do
>> l <- fmap lines getContents
>> putStrLn "ECHO result"
>> putStr $ unlines l
>>
>> If the client logic, that is the part for feeding data to this program
>> and wait for the response, expect the server logic to respond
>> only after all the client's request data are feed to the server logic,
>> the behaviour could be problematic.
>>
>> I'm now writing simple web server, as you might guess, and
>> my web server responds "HTTP/1.1" before any client's request
>> are given. I'm using hGetContents and web server's logic is
>> basically same with above code skeleton.
>>
>> How can I tell the program to respond data only after all the
>> requests are feed? Any directions are welcomed.
>> Thanks.
>>
>> Chul-Woong
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140724/b50c59ce/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 73, Issue 19
*****************************************