<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2017-06-29 15:16 GMT+02:00 Francesco Ariis <span dir="ltr"><<a href="mailto:fa-ml@ariis.it" target="_blank">fa-ml@ariis.it</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="gmail-">On Thu, Jun 29, 2017 at 02:33:22PM +0200, Jona Ekenberg wrote:<br>
> I want to replace lines with the content of a file if the line is a filename<br>
<br>
</span>Hello Jonas, you want to user `return`:<br>
<br>
    λ> :t return<br>
    return :: Monad m => a -> m a<br>
<br>
which lifts a simple value inside a monad (in this case, a -> IO a), like<br>
this:<br>
<br>
    lineOrIO :: String -> IO String<br>
    lineOrIO cs | (isPrefixOf "./" cs) = readFile cs<br>
                | otherwise            = return cs<br>
<br>
If this is not a school assignment, consider replacing `isPrefixOf "./"`<br>
with something from `System.Directory`.<br>
<br>
Does this help?</blockquote><div><br></div><div>Thank you for your help Francesco! </div><div><br></div><div>I tried writing it like this:</div><div><br></div><div>> lineOrIo :: String -> IO String</div><div>> lineOrIo cs | (isPrefixOf "./" cs) = readFile cs</div><div>>             | otherwise            = return cs</div><div>></div><div>> printLines path = do</div><div>>   file <- readFile path</div><div>>   lines <- map lineOrIo (lines file)</div><div>>   print lines</div><div><br></div><div>But when evaluating I get this error:</div><div><br></div><div><div>PrintComments.lhs:20:14-38: error: …</div><div>    • Couldn't match type ‘[]’ with ‘IO’</div><div>      Expected type: IO (IO String)</div><div>        Actual type: [IO String]</div><div>    • In a stmt of a 'do' block: lines <- map lineOrIo (lines file)</div><div>      In the expression:</div><div>        do { file <- readFile path;</div><div>             lines <- map lineOrIo (lines file);</div><div>             print lines }</div><div>      In an equation for ‘printLines’:</div><div>          printLines path</div><div>            = do { file <- readFile path;</div><div>                   lines <- map lineOrIo (lines file);</div><div>                   print lines }</div><div>Compilation failed.</div></div><div><br></div><div>Sadly I am not yet very used to the error messages, so I don't understand what ghci is telling me.</div><div>As far as I can tell (lines file) should give me an array of strings, which I turn into an array of IO String.</div><div>Could that be the error? It shouldn't be [IO String] but instead IO [String]? How do I turn the former into the latter?</div><div><br></div><div>Kind regards,</div><div>Jona</div><div><br></div><div>PS. It is not a school assignment, so I'll make sure to check out System.Directory.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div></div>