Now, I've got the wish list for what it takes to make the solve function really
work. And so, I'm just going to start working
through it. From here on out it's just straight
forward how to design functions recipe and wish list management, until we've got
everything done. Okay I'm in SudokuV2.rkt and the first
wishlist entry that I'm going to work on is solved.
So solved is a function that's supposed to produce true if a board is solved.
So let's do some check expects. Now boards are big so I'm not going to
type boards here if I can help it. Let me remember what we had up here.
And so board 1 is clearly not solved and board 2 is clearly not solved and board
4s is solved. Let's go back down to solve, or one is
not solved, or 2 is not solved. But 4 and 4 S was solved.
Okay. Let's make sure those are well-formed.
What happened here? Oh, way back a long time ago I forgot to
come and up this stub. Okay.
I've got a bunch of failing tests but that's okay.
These tests are failing and also these tests are failing.
So how are we going to template this? Well let's go remind ourselves what the
data definitions are. Let's see, there's a thing called a
value, which is a natural from one to nine.
And then there's a board, and a board is a list where each element of the list is
either a value or false. So let's see, that means a board that's
solved, a board that doesn't have any empty spaces in it, is a board where
everything is natural. Put it another way, it's a board where
nothing is false. So let's see, a board is solved if
everything's a natural. Means we need to go through the board and
make sure everything's a natural. And if it is we produce a boullion, and
we're doing the same check on everything. That's an and map.
I'm going to template this as a call to andnap.
And if you don't remember andnap, go look at andnap right now.
I'll wait 'till you come back. Actually pause and then come back.
And map takes a function calls that function every element of the list, and
produces true if and only if that function produced true every time.
So a board that's full, a board that's solved is a board where every element is
a number, no element is false. That's just this.
Let's try running it. Oops, forgot to comment on that stuff
too. Hmm, two tests fail.
Those tests I think, I hope, are tests on, these are tests on, see the boards
are big so they print out kind of big. These are tests here, those 2 tests are
failing, but our tests on solved are passing.
So, solved is done, solved was easy, solved was just an end map of number over
the board. Okay, now let's work on Next boards.
And we're going to suffer here a little bit, because boards are big.
They're eighty one elements long. So that's going to be a little bit
annoying, when we construct the tests. I think we can manage to do it anyways.
What we need to do is we find the first empty square, we fill it.
We make nine boards withi 1 through 9 in that empty suquare.
And then we keep only the valid ones. So what I'm going to do here is I'm
going to write a check-expect, and the board I'm going to use is going to be
cons of 1 onto rest of board 1. Board 1, if you recall, is the empty
board. Board one is an empty board.
So what I'm saying here is I'm saying make a board, you'd never see this board
in a game in a newspaper, but I'm saying make a board that has a one in the upper
left corner and everything else is empty. And now I need to make the result.
Well lets see the result is going to be a list of boards and you know we can kind
of see what it's going to be. It's going to be, it's not going to be
that board. Because that board's invalid.
Right, once we've got the 1 there, we're not going to have another 1.
But it's basically we're going to use the board 1, 2 and everything else is blank.
List 1, 3 and everything else is blank. And it's going to be those 8 boards.
3, 4, 5, 6, 7, 8, 9. 4, 5, 6, 7, 8, 9.
Now, how am I going to write this dot, dot, dot.
How am I going to write this? Well, this is, this, these dots, are kind
of really something like rest of rest of board 1.
Okay? Except I can't say list.
Like this, I need to say, cons 1 on to cons 2 on to rest of, rest of board 1.
So, I take the first two squares of the board and I replace them with 1 and 2,
So, all of these things hare that I have typed aren't quiet right.
I was doing them to sort of see what it would look like.
I'll just replace them now with let's see that's a 3 and that's a 4 and.
That's five and, yeah, this is a little tedious.
[SOUND] But remember, the more complicated the functions are, the more
it's worth really having thought through what they're going to do.
That's check expects as examples. And the more it's worth having good
tests. That's check expect as tests.
So that if we don't get this right, [SOUND] we'll know about it.
Put it this way [SOUND]. If you've got next boards wrong, then
there is no way solve is going to work, right.
And it's going to be hard debugging solve if the wheel problem is in next boards.
So even those these tests were a bit cumbersome to make, they were worth
making. [SOUND] And I think actually that's
going to be enough in this case. I could make some more complicated cases,
but I think we'll be able to test the helpers to do that.
And the reason I think that is look at this purpose.
We find the first empty square we fill that with natural one to nine.
And we keep only the valid boards. How are we going to template this?
To me, it templates as a function composition.
Starting with the board, we're going to do three distinct things.
Three transformations of what we get to get the result.
So I'm going to template this as define next boards of board.
You can template it this way. I'm going to start with find first blank
of board. And once I've got that blank, I'm
going to say, fill with 1 to 9, the blank spot on the board.
So this 1st thing, find blank, gets me the blank spot.
This next thing says, hey, in this board, fill the blank spot with 1 to 9.
So this first thing produces a single pause.
This second thing produces a list of 9 boards, and I'm going to say keep only
valid of that. And that last thing produces up to 9
boards. I'm templating it as a composition.
To understand the function composition probably the best thing to do now is do
the wishlist entries. So let's see, find blank, it consumes a
board and it produces a pause or a position.
And it produces the position of the first blank square.
And one thing that makes this function's job a little easier is we're only
going to call it on boards that we know aren't full.
Because if you go back here, first we ask hey, is the board solved.
And only if the board is not solved, in other words if it's not full do we call
next-boards. So this function can assume, the board,
has whoops, the board has at least one blank square.
Define, oops, is a wish list entry. Define blank a board.
And we need to produce a position, so let's just say zero stub.
So that's our wish list entry for find blank.
Here is our wish list entry for fill, with 1 to 9.
Let's see. It's 1st argument is going to be a Pos,
and it's 2nd argument is going to be a Board, and it's going to produce, well,
it has to produce a list of Board. We want to take the Board and the blank.
And produce one board for the filling of the blank with one, and a board for the
filling of the blank with two. And a board for filling of the blank with
three and so on dot dot dot. Produce nine boards with, blank filled
with Natural, 1 to 9, it's a wish list entry.
And finally keep only valid. Well, keep only valid consumes a list of
board and it produces a list of board. Produce list containing only valid
boards. It's a wishlist entry and there's the
stub. Lobd is empty.
I didn't mark this as a stub, although it obviously was one, and I'm going to mark
this as a stub. See if we run we'll see if all the stubs
and things are well formed. What happened there?
Oh I forgot to comment out the stub for next-boards.
Fill with 1 to 9 expects only one argument.
Oh, yeah I forgot to give fill, I, I put two in the signature but I only put one
in the stub. So, we'll say, let's see which argument
comes first. The position comes first and then the
board. So let's see we'll just call the position
parameter P. Great.
Now, we've got some failing tests, which isn't so surprising, but things seem to
be well formed. You know, it's kind of one step forward,
two steps back here. We had a wishlist entry for solved, and
we were able to finish solved, so that was great, but we also had a wishlist
entry for Nextboards In when we design next boards, we ended up with three more
wish list entries. But some point they start actually
resolving.