Haskell: [IO ()] to IO ()

Viewed 591

Haskell wiki has the following question:

https://en.wikibooks.org/wiki/Haskell/Higher-order_functions for :: a -> (a -> Bool) -> (a -> a) -> (a -> IO ()) -> IO () for i p f job = -- ???

I was able to come up with the following implementation:

generate :: a -> (a->Bool) -> (a->a) -> [a]
generate s cnd incr = if (cnd s) then [] else [s] ++ generate (incr s) cnd incr

-- collapse :: [IO ()] -> IO ()
-- collapse (x:xs) = x ++ collapse xs
-- does not work ^^^^^^


for::a->(a->Bool)->(a->a)->(a->IO())->IO()
for s cnd incr ioFn = map (ioFn) (generate s cnd incr)

Ofcourse map (ioFn) (generate s cnd incr) results in [IO ()]. I am not sure how this can be transformed to IO () I need something like foldl but the one that works with [IO ()] instead of [a].

1 Answers
Related