[Haskell-beginners] Eliminate repetitive monad/list code

John Smith voldermort at hotmail.com
Thu Nov 4 12:19:50 EDT 2010


I have code which looks like this:

foo :: IO A

bar :: Bool -> A -> Int -> Bool -> IO ()

do
   x <- foo
   y <- foo
   z <- foo

   bar True x 1 False
   bar True y 2 False
   bar True z 3 False

What is the best way to factor it, including eliminating the temporary x,y,z variables? I can get this down to

foo :: IO A

bar :: Bool -> A -> Int -> Bool -> IO ()

bar' a b = bar True a b False

do
   x <- foo
   y <- foo
   z <- foo

   bar x 1
   bar y 2
   bar z 3

but I don't know what to do next. Is there some form of list comprehension or combination of map and lift which will 
eliminate the repetition?



More information about the Beginners mailing list