<span class="Apple-style-span" style="font-family: 'Times New Roman'; 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'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 <- x<br> return result : sequenceIO xs<br></blockquote></div><br><div>The problem is indeed here. The type of 'sequenceIO xs' is IO [a], but the type of result is 'a'. You can't cons an 'a' onto an 'IO [a]'. Thus, what you need is something like this:</div>
</div></span><br>sequenceIO [] = return []<br>sequenceIO (x : xs) = do result <- x<div> xs' <- sequenceIO xs -- to take the list out of the IO Monad<br> return result : xs'<div>
<br><div class="gmail_quote">On Thu, Feb 19, 2009 at 9:56 AM, Sergey V. Mikhanov <span dir="ltr"><<a href="mailto:sergey@mikhanov.com">sergey@mikhanov.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
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] -> 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 => IO a<br>
<br>
ioOne = do print guid<br>
return guid<br>
where<br>
guid = 2<br>
<br>
ioTwo :: Num a => IO a<br>
<br>
ioTwo = do print guid<br>
return guid<br>
where<br>
guid = 3<br>
<br>
sequenceIO :: Num a => [IO a] -> IO [a]<br>
<br>
sequenceIO [] = return []<br>
sequenceIO (x : xs) = do result <- x<br>
return result : sequenceIO xs<br>
<br>
First two functions are there because of the invocation that I've<br>
planned: sequenceIO [getGuid, getNextGuid].<br>
<br>
However, this could not be compiled (GHC):<br>
<br>
Couldn't match expected type `[m a]' against inferred type `IO [a]'<br>
In the second argument of `(:)', namely `sequenceIO xs'<br>
In the expression: return result : sequenceIO xs<br>
In the expression:<br>
do result <- x<br>
return result : sequenceIO xs<br>
<br>
Fine, I thought, something wrong with the type of the 'sequenceIO xs'<br>
(becasue I am sure the type of 'result' 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 => IO a<br>
<br>
bar = do print guid<br>
return guid<br>
where<br>
guid = 2<br>
<br>
foo = do result <- bar<br>
result<br>
<br>
This could not be compiled either:<br>
<br>
No instance for (Num (IO b))<br>
arising from a use of `bar' at auxil.hs:8:19-21<br>
Possible fix: add an instance declaration for (Num (IO b))<br>
In a stmt of a 'do' expression: result <- bar<br>
In the expression:<br>
do result <- bar<br>
result<br>
In the definition of `foo':<br>
foo = do result <- bar<br>
result<br>
<br>
I am a newbie, so I am interpreting this like "Haskell could not<br>
construct Num from the result of invocation of bar, which is of type<br>
IO a". But why do I need this at all? When doing console I/O with<br>
'result <- getLine', 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>