I tried to convert this Scala function to return a lazy stream instead of looking to get all the results and convert them from Seq to Stream when all the results are present. I feel the problem is (for (i <- 1 to 9; z <- solve (xs.updated (pos, i), pos)), output z) toStream .
Any advice is appreciated. Another solution I'm looking at is to return the result when it is found. With this solution, I probably have only 1 result. Thanks
isConflictAt(xs.updated(pos, 0), pos, xs(pos) - constraint checking function.
def solve(xs : List[Int], pos: Int): Stream[List[Int]] = { if (!isConflictAt(xs.updated(pos, 0), pos, xs(pos))) { val pos = xs.indexOf(0) if (pos < 0) {println(xs); Stream(xs) } else (for (i <- 1 to 9; z <- solve(xs.updated(pos, i), pos)) yield z) toStream } else Stream.empty }
source share