Tom Carstens
Tom Carstens

Reputation: 55

IterateUntil with a state parameter

I'm writing a board game for a class. Control.Monad.Loops has an iterateUntil function which is very close to what I want. However, I want to have my action take a parameter (which is the board state for this turn.) So my question is, how idiomatic is the following? Is there anything I could do to remove the explicit recursion?

iterateUntilIO :: (a -> IO a) -> a -> (a -> Bool) -> IO a
iterateUntilIO action state predicate = if predicate state
                                            then return state
                                            else do
                                                nextState <- action state
                                                iterateUntilIO action nextState predicate

Upvotes: 2

Views: 228

Answers (1)

Elliot Robinson
Elliot Robinson

Reputation: 1384

This appears to be iterateUntilM. Your function can be written as:

iterateUntilIO :: (a -> IO a) -> a -> (a -> Bool) -> IO a
iterateUntilIO action state predicate = iterateUntilM predicate action state

This also implies you could simply replace iterateUntilIO with iterateUntilM in your code.

Upvotes: 5

Related Questions