On 21 Feb 2009, at 01:30, Patrick LeBoutillier wrote:

Hi all,

I'm trying to implement the following simple Perl program in Haskell:

 my $nb_tests = 0 ;

 sub ok {
         my $bool = shift ;
         $nb_tests++ ;
         print STDOUT ($bool ? "ok" : "nok") . " $nb_tests\n" ;
 }

 ok(0) ;
 ok(1) ;

The output is:

 nok 1
 ok 2

I'm pretty much a Haskell newbie, but I know a bit about monads (and
have been reading "Real World Haskell"), and I think I need to put the
ok function must live inside some kind of state monad. My problem is
that I also would like the ok function to perform some IO (as shown
above, print).

How is a case like this handled? Can my function live in 2 monads?

I personally wouldn't use two monads at all for this – in fact, I'd only use IO in one function:

main = putStr . unlines . results inputs . snd . tests $ inputs

inputs = [1,2]

tests = foldr (\_ (x,l) -> (not x, x:l)) (True,[])

results = zipWith result
result testN True  = "ok "  ++ show testN
result testN False = "nok " ++ show testN

Bob