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:  [IO String] to IO [String] (Lyndon Maydwell)
   2. Re:  [IO String] to IO [String] (Ovidiu D)


----------------------------------------------------------------------

Message: 1
Date: Mon, 1 Apr 2013 19:17:31 +0800
From: Lyndon Maydwell <[email protected]>
Subject: Re: [Haskell-beginners] [IO String] to IO [String]
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <cam5qztwl5yqwp2yodpgtyvq8ejtwhyafho9nlayckawpw2f...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Ah I see what you're saying.


interact [1] uses hGetContents [2] to read the user-input. This is a
lazy-io function which is also used in readFile [3]. In the same way that
readFile won't read the entire file if its contents aren't used, neither
will interact. There is debate as to weather this is a good way to handle
(lol) IO, with other solutions including Conduit [4] and Pipes [5], but for
simple programs it is an elegant solution.


[1]
http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/System-IO.html#interact
[2]
http://hackage.haskell.org/packages/archive/base/latest/doc/html/System-IO.html#v:hGetContents
[3]
http://hackage.haskell.org/packages/archive/base/4.3.1.0/doc/html/src/System-IO.html#readFile
[4] http://hackage.haskell.org/package/conduit
[5] http://hackage.haskell.org/package/pipes

I hope this helps :)

On Mon, Apr 1, 2013 at 5:54 PM, Ovidiu D <[email protected]> wrote:

> On Mon, Apr 1, 2013 at 11:52 AM, Lyndon Maydwell <[email protected]>wrote:
>
>> The untilexit definition is pretty straightforward. You can see that the
>> the function operates on a string. There are two cases where nothing is
>> returned: "" and "exit" ++ rest.
>>
>>
> Ok, that's clear. What I don't understand is why does it stop interact? It
> returns the same thing if the input is empty or if it starts with "exit".
> Why doesn't it stop interact when the input line is empty?
>
>
>> In order to display a prompt before each input you will need to actually
>> display a prompt /after/ each line, and an additional prompt before you
>> start processing input.
>>
>
> I was trying to do that but I was doing it wrong. Anyway this is the
> result which works as expected now, although I don't really understand why
> it stops at 'exit'
>
> import System.IO
>
> prompt = ">> "
>
> main :: IO ()
> main = do
>     hSetBuffering stdout NoBuffering
>     putStr prompt
>     interact $ display . map reverse . lines . untilexit
>
> display =  concat . map appendPrompt
>     where appendPrompt line = line ++ "\n" ++ prompt
>
>
> untilexit :: String -> String
> untilexit ('e':'x':'i':'t':_)  = []
> untilexit (c:t)               = c : untilexit t
> untilexit []                  = []
>
> _______________________________________________
> 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/20130401/119672e4/attachment.html>

------------------------------

Message: 2
Date: Mon, 1 Apr 2013 15:37:07 +0300
From: Ovidiu D <[email protected]>
Subject: Re: [Haskell-beginners] [IO String] to IO [String]
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <CAKVsE7sV9OV7QsdU3jv8dhgexpyQq4eF=n6vm+-n73zutg2...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

I understand. Thanks a lot for the explanation and for the documentation.

ovidiu


On Mon, Apr 1, 2013 at 2:17 PM, Lyndon Maydwell <[email protected]> wrote:

> Ah I see what you're saying.
>
>
> interact [1] uses hGetContents [2] to read the user-input. This is a
> lazy-io function which is also used in readFile [3]. In the same way that
> readFile won't read the entire file if its contents aren't used, neither
> will interact. There is debate as to weather this is a good way to handle
> (lol) IO, with other solutions including Conduit [4] and Pipes [5], but for
> simple programs it is an elegant solution.
>
>
> [1]
> http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/System-IO.html#interact
> [2]
> http://hackage.haskell.org/packages/archive/base/latest/doc/html/System-IO.html#v:hGetContents
> [3]
> http://hackage.haskell.org/packages/archive/base/4.3.1.0/doc/html/src/System-IO.html#readFile
> [4] http://hackage.haskell.org/package/conduit
> [5] http://hackage.haskell.org/package/pipes
>
> I hope this helps :)
>
> On Mon, Apr 1, 2013 at 5:54 PM, Ovidiu D <[email protected]> wrote:
>
>> On Mon, Apr 1, 2013 at 11:52 AM, Lyndon Maydwell <[email protected]>wrote:
>>
>>> The untilexit definition is pretty straightforward. You can see that the
>>> the function operates on a string. There are two cases where nothing is
>>> returned: "" and "exit" ++ rest.
>>>
>>>
>> Ok, that's clear. What I don't understand is why does it stop interact?
>> It returns the same thing if the input is empty or if it starts with
>> "exit". Why doesn't it stop interact when the input line is empty?
>>
>>
>>> In order to display a prompt before each input you will need to actually
>>> display a prompt /after/ each line, and an additional prompt before you
>>> start processing input.
>>>
>>
>> I was trying to do that but I was doing it wrong. Anyway this is the
>> result which works as expected now, although I don't really understand why
>> it stops at 'exit'
>>
>> import System.IO
>>
>> prompt = ">> "
>>
>> main :: IO ()
>> main = do
>>     hSetBuffering stdout NoBuffering
>>     putStr prompt
>>     interact $ display . map reverse . lines . untilexit
>>
>> display =  concat . map appendPrompt
>>     where appendPrompt line = line ++ "\n" ++ prompt
>>
>>
>> untilexit :: String -> String
>> untilexit ('e':'x':'i':'t':_)  = []
>> untilexit (c:t)               = c : untilexit t
>> untilexit []                  = []
>>
>> _______________________________________________
>> 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/20130401/ecf8ab3a/attachment-0001.htm>

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 58, Issue 5
****************************************

Reply via email to