[Haskell-beginners] [IO String] to IO [String]

Lyndon Maydwell maydwell at gmail.com
Mon Apr 1 10:52:26 CEST 2013


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 <ovidiudeac at gmail.com> 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 <maydwell at gmail.com>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 <ovidiudeac at gmail.com> 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 <maydwell at gmail.com>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 <ky3 at atamo.com> wrote:
>>>>
>>>>> On Sun, Mar 31, 2013 at 5:19 PM, Ovidiu D <ovidiudeac at gmail.com>
>>>>> 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
>>>>> Beginners at haskell.org
>>>>> http://www.haskell.org/mailman/listinfo/beginners
>>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> Beginners mailing list
>>>> Beginners at haskell.org
>>>> http://www.haskell.org/mailman/listinfo/beginners
>>>>
>>>>
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> Beginners at haskell.org
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>>
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> 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.htm>


More information about the Beginners mailing list