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 16:52:26 +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:
<CAM5QZtx-TU8ERdhfp7Zk3wbVde=fqyexdbug+r-cj88zqob...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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.
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.
On Mon, Apr 1, 2013 at 4:15 PM, Ovidiu D <[email protected]> wrote:
> Thanks.
>
> I can see it working but I don't undestand why does untilexit actually
> stop the processing. Can you explain that?
>
> Also how could I display a prompt before reading each line?
>
>
>
> On Mon, Apr 1, 2013 at 7:14 AM, Lyndon Maydwell <[email protected]>wrote:
>
>> Interact should be able to handle line-by-line interaction as per this
>> example, it should even be able to handle the exit case thanks to laziness:
>>
>> > main :: IO ()
>> > main = interact (unlines . map reverse . lines . untilexit)
>> >
>> > untilexit :: String -> String
>> > untilexit ('e':'x':'i':'t':_) = []
>> > untilexit (c:t) = c : untilexit t
>> > untilexit [] = []
>>
>> There will be a lot of things that it won't be able to do however.
>>
>>
>> On Mon, Apr 1, 2013 at 5:49 AM, Ovidiu D <[email protected]> wrote:
>>
>>> My problem with interact was that it doesn't give me the line when the
>>> user hits enter but instead it gives me all the lines at once when stdin is
>>> closed (unless I did something wrong)
>>>
>>> The other problem is that I want to stop the command processing when the
>>> user types the command "exit" and it seems interact can't do that.
>>>
>>>
>>> On Sun, Mar 31, 2013 at 2:52 PM, Lyndon Maydwell <[email protected]>wrote:
>>>
>>>> Depending on what you're doing with the lines, it may be worth checking
>>>> out the `interact` function as well :-)
>>>>
>>>>
>>>> On Sun, Mar 31, 2013 at 7:42 PM, Kim-Ee Yeoh <[email protected]> wrote:
>>>>
>>>>> On Sun, Mar 31, 2013 at 5:19 PM, Ovidiu D <[email protected]>
>>>>> wrote:
>>>>> > I would like to make this function to have the signature
>>>>> > f : IO [String]
>>>>> > ...such that I can get rid of the IO monad and pass the pure string
>>>>> list to
>>>>> > the processing function.
>>>>>
>>>>> You could use:
>>>>>
>>>>> getContents >>= lines :: IO [String]
>>>>>
>>>>> -- Kim-Ee
>>>>>
>>>>> _______________________________________________
>>>>> Beginners mailing list
>>>>> [email protected]
>>>>> http://www.haskell.org/mailman/listinfo/beginners
>>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> Beginners mailing list
>>>> [email protected]
>>>> http://www.haskell.org/mailman/listinfo/beginners
>>>>
>>>>
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>>
>>
>> _______________________________________________
>> 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/9ac7f410/attachment.html>
------------------------------
Message: 2
Date: Mon, 1 Apr 2013 12:54:57 +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:
<cakvse7ubqqtxnu5ifyqszupmdgr6j3hj50_wqzx4pyvym0d...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
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 [] = []
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130401/8c10f6d4/attachment-0001.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 58, Issue 4
****************************************