<div dir="ltr"><div><div>Doing it the way you are trying to do it breaks the IO abstraction.  In order to do it you&#39;d have to use unsafe functions.  Unsafe functions are bad.  I&#39;m not going to explain why but they tend to bite you as your program gets more complex and weirdness starts to occur, like threads ceasing operation while awaiting input is something that bit me when I went down that route.  So let me explain how I would do it using both pipes and conduits as examples:<br>
<br>import Data.Conduit as C hiding ((&gt;+&gt;), runPipe)<br>import System.IO<br>import Control.Monad.Trans<br>import Text.Printf.Mauke<br><br>import Control.Pipe as P<br>import Control.Monad (forever)<br><br>-- Source runs in the IO monad and produces Strings<br>
commandSource :: Source IO String<br>commandSource = do<br>  command &lt;- liftIO getLine<br>  if command == &quot;exit&quot;<br>    then return ()<br>    else do<br>      C.yield command<br>      commandSource -- loop to fetching new values to send down the pipe<br>
<br>-- Sink runs in the IO monad and takes any printfable argument and returns () when pipe completes.<br>displaySink :: PrintfArg a =&gt; Sink a IO ()<br>displaySink = do<br>  m &lt;- C.await<br>  case m of<br>    Nothing -&gt; return ()  -- if nothing comes in, just exit<br>
    Just x -&gt; do<br>      liftIO $ printf &quot;Command not implemented (conduit): &#39;%s&#39;\n&quot; x<br>      displaySink<br>      <br>main = do<br>  hSetBuffering stdout NoBuffering<br>  commandSource $$ displaySink<br>
  runPipe $ commandProducer &gt;+&gt; displayConsumer<br>  <br><br>commandProducer :: PrintfArg a =&gt; Producer a String IO ()<br>commandProducer = do<br>  x &lt;- lift getLine<br>  if x == &quot;exit&quot;<br>    then return ()<br>
    else P.yield x &gt;&gt; commandProducer<br>    <br>displayConsumer :: Consumer String IO ()<br>displayConsumer = forever $ P.await &gt;&gt;= lift . printf &quot;Command not implemented (pipes): &#39;%s&#39;\n&quot;<br>
<br></div>There are some utility function to shorten some of these definitions a bit in conduit.  These two examples are equivalent.  But basically you are creating a pipeline, the first of which gets commands until it gets an exit and then sends them down the pipeline (as a string).  The second piece of the pipe accepts anything that is printfable and prints it.  It will stop when the upstream stops sending it strings to print.  The point here is that you have little functions that you can compose together with other functions and create something bigger where none of the pieces interfere with each other or break the IO abstraction.<br>
<br></div>As to which of these libraries you should try?  Conduits is a bit more straight forward and has a lot more documentation and supporting libraries.  Pipes is a lot more flexible in that you could send things both directions along the pipe in the future when you become proficient with the library.<br>
<div><br><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sun, Mar 31, 2013 at 9:38 PM, Ovidiu D <span dir="ltr">&lt;<a href="mailto:ovidiudeac@gmail.com" target="_blank">ovidiudeac@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>I&#39;m not sure I understand what you mean by &quot;I know you have the best intentions in writing this, but there are pitfalls.&quot;. Anyway, here&#39;s the code which doesn&#39;t work apparently because mapM is waiting for the whole list before it goes further.<br>

<br>prompt = &quot;&gt;&gt; &quot;<br><br>commands :: [IO String]<br>commands = readCommand : commands<br>    where readCommand = putStr prompt &gt;&gt; getLine<br><br>display :: Show a =&gt; [ a ] -&gt; IO ()<br>display = mapM_ $ putStr . show<br>

<br>executeCommand :: String -&gt; String<br>executeCommand = printf &quot;Command not implemented: &#39;%s&#39;&quot;<br><br>processCommands :: [IO String] -&gt; IO [ String ]<br>processCommands = mapM processOneCommand<br>

    where processOneCommand cmd = cmd &gt;&gt;= (return . executeCommand )<br><br>main =<br>    hSetBuffering stdout NoBuffering<br>    &gt;&gt; processCommands commands<br>    &gt;&gt;= display<br><br></div><div>This is just for learning purposes and I&#39;m looking for the &quot;haskell way to do it&quot;. My intention is to write the function processCommands such that it takes the decision to either fetch the next command from the command list (i.e. console) or to exit the application.  <br>

</div><div><br></div>Regarding your comment &quot;Just know that at some point you should learn to use conduits or pipes for a much better approach to modeling things like this.&quot;. Can you point me to some documentation? <br>

<br>Thanks!<br></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Apr 1, 2013 at 3:53 AM, David McBride <span dir="ltr">&lt;<a href="mailto:toad3k@gmail.com" target="_blank">toad3k@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div>I know you have the best intentions in writing this, but there are pitfalls.  Unexpected things happen when you interleave IO in this manner, but nonetheless, here&#39;s how you would do it.<br>


<br>myGetLine = do<br>  x &lt;- getLine<br>  if (x == &quot;exit&quot;)<br>      then return []<br>      else do<br>        xs &lt;- unsafeInterleaveIO myGetLine<br>        return (x:xs)<br><br>main = do<br>  x &lt;- myGetLine<br>


</div><div>  print x<br><br></div><div>Just know that at some point you should learn to use conduits or pipes for a much better approach to modeling things like this.<br></div><div><br></div></div></div></div><div class="gmail_extra">


<br><br><div class="gmail_quote"><div>On Sun, Mar 31, 2013 at 7:26 PM, Ovidiu D <span dir="ltr">&lt;<a href="mailto:ovidiudeac@gmail.com" target="_blank">ovidiudeac@gmail.com</a>&gt;</span> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div><div>
<div dir="ltr"><div><div><div><div>Hi again,<br><br>Given the following code:<br><br>g :: IO String -&gt; IO String<br><br>f :: [IO String] -&gt; IO [ String ]<br>f = mapM g<br></div><div><br>The implementation of f is wrong because I would like to:<br>



</div><div>1. Make f behave lazy<br></div><div>Its input list is made of lines read from stdin and I want it to process lines one by one as they are entered by the user.<br><br></div><div>2. Implement  f such that it stops consuming items from the input list when the input item meets some condition. For example:<br>



isExit item = (&quot;exit&quot; == item)<br></div><div><br>I tried to implement my own custom iteration by recursion but I got stuck in the combination of IO and list monads.<br><br></div><div>Any help is appreciated.<br>



<br></div><div>Thanks!<br></div><div><br></div></div></div></div></div>
<br></div></div><div>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></div></blockquote></div><br></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>