<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On 21 Feb 2009, at 01:30, Patrick LeBoutillier wrote:</div><br><blockquote type="cite"><div>Hi all,<br><br>I'm trying to implement the following simple Perl program in Haskell:<br><br> my $nb_tests = 0 ;<br><br> sub ok {<br> my $bool = shift ;<br> $nb_tests++ ;<br> print STDOUT ($bool ? "ok" : "nok") . " $nb_tests\n" ;<br> }<br><br> ok(0) ;<br> ok(1) ;<br><br>The output is:<br><br> nok 1<br> ok 2<br><br>I'm pretty much a Haskell newbie, but I know a bit about monads (and<br>have been reading "Real World Haskell"), and I think I need to put the<br>ok function must live inside some kind of state monad. My problem is<br>that I also would like the ok function to perform some IO (as shown<br>above, print).<br><br>How is a case like this handled? Can my function live in 2 monads?<br></div></blockquote></div><br><div>I personally wouldn't use two monads at all for this – in fact, I'd only use IO in one function:</div><div><br></div><div><div><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;">main = putStr . unlines . results inputs . snd . tests $ inputs</span></font></div><div><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;"><br></span></font></div><div><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;">inputs = [1,2]</span></font></div><div><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;"><br></span></font></div><div><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;">tests = foldr (\_ (x,l) -> (not x, x:l)) (True,[])</span></font></div><div><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;"><br></span></font></div><div><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;">results = zipWith result</span></font></div><div><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;">result testN True = "ok " ++ show testN</span></font></div><div><font class="Apple-style-span" face="Monaco" size="3"><span class="Apple-style-span" style="font-size: 11px;">result testN False = "nok " ++ show testN</span></font></div><br></div><div>Bob</div></body></html>