[Haskell-beginners] gtk2hs timeoutSeq

Norbert Wojtowicz wojtowicz.norbert at gmail.com
Mon Dec 15 14:02:31 EST 2008


I'm trying to use a sequence of timeoutAdd's in a gtk2hs app. By
sequence, I mean one (IO Bool) must return as False before the next

timeoutSeq [ (fn1, 100), (fn2, 200) ]

fn1 will keep running every 100, until it returns False. Then fn2 will
get a timeoutAdd 200, and so on...

First attempt (works fine, I think):

timeoutSeq :: [(IO Bool, Int)] -> IO ()
timeoutSeq [] = return ()
timeoutSeq ((fn,n):rest) = do
  timeoutAdd (aux fn rest) n
  return ()
      aux :: IO Bool -> [(IO Bool, Int)] -> IO Bool
      aux fn [] = fn
      aux fn lst = do
            r <- fn
            case r of
              True  -> return True
              False -> do
                     timeoutSeq lst
                     return False

Now, I'd like to write a timeoutDep (fn1, n1) (fn2, n2)  where fn1 and
fn2 are both running at their respective rates, and fn1 will run only
as long as fn2 is running. The use case for this is eg: Run fn1 often
and fn2 less often, but I only care fn1 to be running as long as fn2
is still valid.

timeoutDep (fn,n) (fn2,n2) = do
  hdl <- timeoutAdd fn n
  timeoutSeq [(aux fn2 hdl, n2), (return False, 100)]
      aux fn2 hdl = do
        r <- fn2
        case r of
          True  -> return True
          False -> do
                 timeoutRemove hdl
                 return False

This implementation sort of works, except it exits immiediately (and
as IO ()), so I can't for example do this:

  timeoutSeq [((timeoutDep (print "a" >> return True, 100)
                                                  (print "b" >> return
True, 100)), 100)
                         , (print "c" >> return False, 100)]

Ths silly example should keep printing 'a' and 'b' w/o ever printing
'c'.  Any ideas or suggestions?

More information about the Beginners mailing list