<span class="Apple-style-span" style="font-family: &#39;Times New Roman&#39;; font-size: 16px; "><div style="margin-top: 8px; margin-right: 8px; margin-bottom: 8px; margin-left: 8px; font: normal normal normal small/normal arial; ">
You did really well here. There&#39;s just one small detail that you missed, which is causing the problem:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<br>sequenceIO [] = return []<br>sequenceIO (x : xs) = do result &lt;- x<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result : sequenceIO xs<br></blockquote></div><br><div>The problem is indeed here. The type of &#39;sequenceIO xs&#39; is IO [a], but the type of result is &#39;a&#39;. You can&#39;t cons an &#39;a&#39; onto an &#39;IO [a]&#39;. Thus, what you need is something like this:</div>
</div></span><br>sequenceIO [] = return []<br>sequenceIO (x : xs) = do result &lt;- x<div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xs&#39; &lt;- sequenceIO xs -- to take the list out of the IO Monad<br>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result : xs&#39;<div>
<br><div class="gmail_quote">On Thu, Feb 19, 2009 at 9:56 AM, Sergey V. Mikhanov <span dir="ltr">&lt;<a href="mailto:sergey@mikhanov.com">sergey@mikhanov.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
 &nbsp; Hi community,<br>
<br>
I am making my first steps in Haskell, reading Haskell wikibook and<br>
now stuck with one of the excercises, namely this one:<br>
<br>
Implement a function sequenceIO :: [IO a] -&gt; IO [a]. Given a list of<br>
actions, this function runs each of the actions in order and returns<br>
all their results as a list.<br>
<br>
This is what I came with:<br>
<br>
ioOne :: Num a =&gt; IO a<br>
<br>
ioOne = do print guid<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return guid<br>
 &nbsp; &nbsp; &nbsp; &nbsp;where<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; guid = 2<br>
<br>
ioTwo :: Num a =&gt; IO a<br>
<br>
ioTwo = do print guid<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return guid<br>
 &nbsp; &nbsp; &nbsp; &nbsp;where<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; guid = 3<br>
<br>
sequenceIO :: Num a =&gt; [IO a] -&gt; IO [a]<br>
<br>
sequenceIO [] = return []<br>
sequenceIO (x : xs) = do result &lt;- x<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result : sequenceIO xs<br>
<br>
First two functions are there because of the invocation that I&#39;ve<br>
planned: sequenceIO [getGuid, getNextGuid].<br>
<br>
However, this could not be compiled (GHC):<br>
<br>
Couldn&#39;t match expected type `[m a]&#39; against inferred type `IO [a]&#39;<br>
In the second argument of `(:)&#39;, namely `sequenceIO xs&#39;<br>
In the expression: return result : sequenceIO xs<br>
In the expression:<br>
 &nbsp; &nbsp;do result &lt;- x<br>
 &nbsp; &nbsp; &nbsp; &nbsp;return result : sequenceIO xs<br>
<br>
Fine, I thought, something wrong with the type of the &#39;sequenceIO xs&#39;<br>
(becasue I am sure the type of &#39;result&#39; is fine). So I wrote another<br>
program to check what happens to the result of IO action evaluation<br>
(namely, which type is assigned):<br>
<br>
bar :: Num a =&gt; IO a<br>
<br>
bar = do print guid<br>
 &nbsp; &nbsp; &nbsp; &nbsp; return guid<br>
 &nbsp; &nbsp; &nbsp;where<br>
 &nbsp; &nbsp; &nbsp; &nbsp; guid = 2<br>
<br>
foo = do result &lt;- bar<br>
 &nbsp; &nbsp; &nbsp; &nbsp; result<br>
<br>
This could not be compiled either:<br>
<br>
No instance for (Num (IO b))<br>
 &nbsp; &nbsp;arising from a use of `bar&#39; at auxil.hs:8:19-21<br>
Possible fix: add an instance declaration for (Num (IO b))<br>
In a stmt of a &#39;do&#39; expression: result &lt;- bar<br>
In the expression:<br>
 &nbsp; &nbsp;do result &lt;- bar<br>
 &nbsp; &nbsp; &nbsp; &nbsp;result<br>
In the definition of `foo&#39;:<br>
 &nbsp; &nbsp;foo = do result &lt;- bar<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result<br>
<br>
I am a newbie, so I am interpreting this like &quot;Haskell could not<br>
construct Num from the result of invocation of bar, which is of type<br>
IO a&quot;. But why do I need this at all? When doing console I/O with<br>
&#39;result &lt;- getLine&#39;, I do not need to reconstruct String from the<br>
result.<br>
<br>
What am I doing wrong? Where is the failure in reasoning?<br>
<br>
Thanks,<br>
Sergey<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>
</blockquote></div><br></div></div>