<div dir="ltr">On Wed, Sep 4, 2013 at 10:21 AM, yi lu <span dir="ltr">&lt;<a href="mailto:zhiwudazhanjiangshi@gmail.com" target="_blank">zhiwudazhanjiangshi@gmail.com</a>&gt;</span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div>I want to read a text file, and store it in a <b>String</b>. But readFile will get <b>IO String</b>. I search with google and they tell me it is not necessarily to do so. Can you explain to me why is this? Furthermore, How to read a file and store it in a String?<br>
</div></div></blockquote><div><br></div><div>You do not do so directly. An IO action is a promise to produce a value, not an actual value. (readFile contains a String in the same way the &quot;ls&quot; or &quot;dir&quot; command contains a list of files.)</div>
<div><br></div><div>I suggest you take a look at <a href="http://learnyouahaskell.com/input-and-output#files-and-streams">http://learnyouahaskell.com/input-and-output#files-and-streams</a> to see how IO works in Haskell.</div>
<div><br></div><div>tl;dr: use do notation (which lets you pretend to a limited extent that you can see the String in an IO String) or &gt;&gt;= or fmap to attach a callback to the IO &quot;promise&quot;.</div><div><br></div>
<div>    readFile &gt;&gt;= (something that operates on a String and produces an IO whatever)</div><div><br></div><div>    do s &lt;- readFile</div><div>       (something that operates on a String and produces an IO whatever)</div>
<div><br></div><div>Note that in the end it&#39;s still in IO. You can&#39;t escape it. (There are actually ways to &quot;escape&quot; but they will get you into trouble fairly quickly because they don&#39;t work the way you want them to.)</div>
<div><br></div></div>-- <br><div dir="ltr"><div>brandon s allbery kf8nh                               sine nomine associates</div><div><a href="mailto:allbery.b@gmail.com" target="_blank">allbery.b@gmail.com</a>                                  <a href="mailto:ballbery@sinenomine.net" target="_blank">ballbery@sinenomine.net</a></div>
<div>unix, openafs, kerberos, infrastructure, xmonad        <a href="http://sinenomine.net" target="_blank">http://sinenomine.net</a></div></div>
</div></div>