Idiomatic stateful loop in Haskell

  • A+
Category:Languages

Is there an idiomatic way to express following code in Haskell?

main :: IO () main = loop initState1 initState2 initState3  loop :: State1 -> State2 -> State3 -> IO () loop s1 s2 s3 = do   s1' <- runService1 s1   s2' <- runService2 s2   s3' <- runService3 s3   loop s1' s2' s3' 

This code is very verbose so I probably doing something weird.

 


main = fix (zipWithM ($) >=>)   [runService1, runService2, runService3]   [initState1 , initState2 , initState3 ] 

Compare fix . (>>=) :: IO a -> IO b, which is forever.

Edit: This only works if State1 = State2 = State3. If not, data-fix allows:

main = fix (traverse unFix >=>)   [ana runService1 initState1, ana runService2 initState2, ana runService3 initState3] 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: