<div dir="ltr"><div><div><div><div><br></div>Please correct my inference if I'm wrong:<br><br></div>An IO Integer is not an integer, it's a promise to read an Integer later. The "sequence" function tells the runtime it's time to make good on that promise.<br><br></div>Sound about right?<br><br></div><div>Thanks!<br></div><div><div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Oct 20, 2015 at 9:15 AM, Petr Vápenka <span dir="ltr"><<a href="mailto:petr.vapenka@gmail.com" target="_blank">petr.vapenka@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello Dan,<div><br></div><div>`IO Integer` is something that, when executed, returns and `Integer` and there is no instance of `Show` for `IO Integer` as the compiler says.</div><div><br></div><div>You have to run the computations that will return the numbers and then print them, like so:</div><div><br></div><div><span class=""><div><span style="font-size:12.8px">main :: IO ()</span></div><div><span style="font-size:12.8px">main = do</span></div><div><span style="font-size:12.8px"> let filenames = ["/etc/services"]</span></div></span><div><span style="font-size:12.8px"> let ioSizes = map get_size filenames :: [IO Integer]</span></div><div><span style="font-size:12.8px"> sizes <- sequence ioSizes</span></div><div><span style="font-size:12.8px"> mapM_ print sizes</span></div></div><div><br></div><div>-- sequence :: Monad m => [m a] -> m [a]</div><div><br></div><div>One important part is the use of sequence which transforms (ioSizes :: [IO Integer]) to `IO [Integer]` that is run and the result bound to (sizes : [Integer]).</div><div><br></div><div>Hope that's clear enough to get the point :)</div><div><br></div><div>Petr</div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Tue, Oct 20, 2015 at 5:58 PM, Dan Stromberg <span dir="ltr"><<a href="mailto:strombrg@gmail.com" target="_blank">strombrg@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div dir="ltr"><div><div><br></div>Here's a small program that replicates the compilation issue I'm seeing:<br><br>import qualified System.Posix.Files<br><br>get_size :: String -> IO Integer<br>get_size filename = do<br> file_status <- System.Posix.Files.getFileStatus filename<br> let file_size = System.Posix.Files.fileSize file_status<br> let integer_file_size = fromIntegral file_size<br> return integer_file_size<br><br>main :: IO ()<br>main = do<br> let filenames = ["/etc/services"]<br> let sizes = map get_size filenames<br> mapM_ print sizes<br><br></div>The compilation error I get is:<br><br>ghc -Wall --make -o stat2 stat2.hs<br>[1 of 1] Compiling Main ( stat2.hs, stat2.o )<br><br>stat2.hs:15:11:<br> No instance for (Show (IO Integer)) arising from a use of `print'<br> Possible fix: add an instance declaration for (Show (IO Integer))<br> In the first argument of `mapM_', namely `print'<br> In a stmt of a 'do' block: mapM_ print sizes<br> In the expression:<br> do { let filenames = ...;<br> let sizes = map get_size filenames;<br> mapM_ print sizes }<br>make: *** [stat2] Error 1<br><br clear="all"><div><div>I've googled quite a bit, and guessed quite a bit, and added type declarations some, but I'm still not converging on a solution.<br><br></div><div>Why can't I print an IO Integer?<br><br></div><div>Thanks!<span><font color="#888888"><br><br></font></span></div><span><font color="#888888"><div>-- <br><div>Dan Stromberg</div>
</div></font></span></div></div>
<br></div></div>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">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-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>
<br>_______________________________________________<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-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">Dan Stromberg</div>
</div></div></div></div></div></div>