Converted Python backtracking pseudocode to valid generators.