Hi, let’s say I have a recursive loop - it’s a crosswords solver, could be a sudoku/scrabble/any BFS recursive solver.
- I know how to print at each recursion. My function
recurse
has a-> IO()
return type - What I don’t know is to print “at most each second”. And haven’t tried using an MVar yet.
- Kudos it this also helps me terminate the recursion after N solutions
Scenario, let’s say the function does roughly
recurse matrix = let
-- computing child outcomes
children = allNextStates matrix
in do
print matrix
mapM_ recurse children
I’d like this function to “print only if one second is elapsed till the last print”. What is your advice ? today I can imagine a solution based on an additional MVar
parameter which would contain lastTimeStamp
and compute now
. Upon printing again, the MVar would get updated. This would also allow me to store a solutionsCount
to exit early from the recursion when some threshold goal has been reached.
FWIW the code is here and a working JS equivalent can be found here. Any trivial or standard way of doing this other than an MVar parameter ?