1 00:00:06,140 --> 00:00:09,890 Now, I've got the wish list for what it takes to make the solve function really 2 00:00:09,890 --> 00:00:12,220 work. And so, I'm just going to start working 3 00:00:12,220 --> 00:00:15,370 through it. From here on out it's just straight 4 00:00:15,370 --> 00:00:20,090 forward how to design functions recipe and wish list management, until we've got 5 00:00:20,090 --> 00:00:25,555 everything done. Okay I'm in SudokuV2.rkt and the first 6 00:00:25,555 --> 00:00:29,480 wishlist entry that I'm going to work on is solved. 7 00:00:29,480 --> 00:00:33,830 So solved is a function that's supposed to produce true if a board is solved. 8 00:00:33,830 --> 00:00:40,360 So let's do some check expects. Now boards are big so I'm not going to 9 00:00:40,360 --> 00:00:44,980 type boards here if I can help it. Let me remember what we had up here. 10 00:00:44,980 --> 00:00:52,330 And so board 1 is clearly not solved and board 2 is clearly not solved and board 11 00:00:52,330 --> 00:01:01,890 4s is solved. Let's go back down to solve, or one is 12 00:01:01,890 --> 00:01:18,597 not solved, or 2 is not solved. But 4 and 4 S was solved. 13 00:01:20,690 --> 00:01:23,804 Okay. Let's make sure those are well-formed. 14 00:01:23,804 --> 00:01:28,266 What happened here? Oh, way back a long time ago I forgot to 15 00:01:28,266 --> 00:01:30,303 come and up this stub. Okay. 16 00:01:30,303 --> 00:01:34,377 I've got a bunch of failing tests but that's okay. 17 00:01:34,377 --> 00:01:39,072 These tests are failing and also these tests are failing. 18 00:01:39,072 --> 00:01:43,820 So how are we going to template this? Well let's go remind ourselves what the 19 00:01:43,820 --> 00:01:48,810 data definitions are. Let's see, there's a thing called a 20 00:01:48,810 --> 00:01:51,260 value, which is a natural from one to nine. 21 00:01:52,630 --> 00:01:57,300 And then there's a board, and a board is a list where each element of the list is 22 00:01:57,300 --> 00:02:06,950 either a value or false. So let's see, that means a board that's 23 00:02:06,950 --> 00:02:12,620 solved, a board that doesn't have any empty spaces in it, is a board where 24 00:02:12,620 --> 00:02:16,330 everything is natural. Put it another way, it's a board where 25 00:02:16,330 --> 00:02:22,060 nothing is false. So let's see, a board is solved if 26 00:02:22,060 --> 00:02:32,020 everything's a natural. Means we need to go through the board and 27 00:02:32,020 --> 00:02:37,460 make sure everything's a natural. And if it is we produce a boullion, and 28 00:02:37,460 --> 00:02:43,430 we're doing the same check on everything. That's an and map. 29 00:02:43,430 --> 00:02:50,600 I'm going to template this as a call to andnap. 30 00:02:50,600 --> 00:02:54,630 And if you don't remember andnap, go look at andnap right now. 31 00:02:54,630 --> 00:03:02,160 I'll wait 'till you come back. Actually pause and then come back. 32 00:03:02,160 --> 00:03:05,980 And map takes a function calls that function every element of the list, and 33 00:03:05,980 --> 00:03:11,040 produces true if and only if that function produced true every time. 34 00:03:11,040 --> 00:03:16,130 So a board that's full, a board that's solved is a board where every element is 35 00:03:16,130 --> 00:03:21,450 a number, no element is false. That's just this. 36 00:03:21,450 --> 00:03:25,280 Let's try running it. Oops, forgot to comment on that stuff 37 00:03:25,280 --> 00:03:33,210 too. Hmm, two tests fail. 38 00:03:33,210 --> 00:03:44,150 Those tests I think, I hope, are tests on, these are tests on, see the boards 39 00:03:44,150 --> 00:03:48,940 are big so they print out kind of big. These are tests here, those 2 tests are 40 00:03:48,940 --> 00:03:53,720 failing, but our tests on solved are passing. 41 00:03:53,720 --> 00:03:57,670 So, solved is done, solved was easy, solved was just an end map of number over 42 00:03:57,670 --> 00:04:03,330 the board. Okay, now let's work on Next boards. 43 00:04:04,540 --> 00:04:07,960 And we're going to suffer here a little bit, because boards are big. 44 00:04:07,960 --> 00:04:12,400 They're eighty one elements long. So that's going to be a little bit 45 00:04:12,400 --> 00:04:17,560 annoying, when we construct the tests. I think we can manage to do it anyways. 46 00:04:17,560 --> 00:04:21,470 What we need to do is we find the first empty square, we fill it. 47 00:04:21,470 --> 00:04:25,300 We make nine boards withi 1 through 9 in that empty suquare. 48 00:04:25,300 --> 00:04:29,580 And then we keep only the valid ones. So what I'm going to do here is I'm 49 00:04:29,580 --> 00:04:32,924 going to write a check-expect, and the board I'm going to use is going to be 50 00:04:32,924 --> 00:04:45,524 cons of 1 onto rest of board 1. Board 1, if you recall, is the empty 51 00:04:45,524 --> 00:04:55,840 board. Board one is an empty board. 52 00:04:55,840 --> 00:05:02,510 So what I'm saying here is I'm saying make a board, you'd never see this board 53 00:05:02,510 --> 00:05:07,780 in a game in a newspaper, but I'm saying make a board that has a one in the upper 54 00:05:07,780 --> 00:05:12,175 left corner and everything else is empty. And now I need to make the result. 55 00:05:12,175 --> 00:05:18,080 Well lets see the result is going to be a list of boards and you know we can kind 56 00:05:18,080 --> 00:05:23,410 of see what it's going to be. It's going to be, it's not going to be 57 00:05:23,410 --> 00:05:29,110 that board. Because that board's invalid. 58 00:05:29,110 --> 00:05:32,450 Right, once we've got the 1 there, we're not going to have another 1. 59 00:05:32,450 --> 00:05:36,576 But it's basically we're going to use the board 1, 2 and everything else is blank. 60 00:05:36,576 --> 00:05:45,981 List 1, 3 and everything else is blank. And it's going to be those 8 boards. 61 00:05:45,981 --> 00:05:59,050 3, 4, 5, 6, 7, 8, 9. 4, 5, 6, 7, 8, 9. 62 00:05:59,050 --> 00:06:00,860 Now, how am I going to write this dot, dot, dot. 63 00:06:00,860 --> 00:06:05,800 How am I going to write this? Well, this is, this, these dots, are kind 64 00:06:05,800 --> 00:06:10,693 of really something like rest of rest of board 1. 65 00:06:10,693 --> 00:06:15,020 Okay? Except I can't say list. 66 00:06:15,020 --> 00:06:25,020 Like this, I need to say, cons 1 on to cons 2 on to rest of, rest of board 1. 67 00:06:25,020 --> 00:06:31,110 So, I take the first two squares of the board and I replace them with 1 and 2, 68 00:06:31,110 --> 00:06:34,000 So, all of these things hare that I have typed aren't quiet right. 69 00:06:34,000 --> 00:06:37,410 I was doing them to sort of see what it would look like. 70 00:06:37,410 --> 00:06:48,431 I'll just replace them now with let's see that's a 3 and that's a 4 and. 71 00:06:48,431 --> 00:06:54,902 That's five and, yeah, this is a little tedious. 72 00:06:54,902 --> 00:07:02,940 [SOUND] But remember, the more complicated the functions are, the more 73 00:07:02,940 --> 00:07:05,970 it's worth really having thought through what they're going to do. 74 00:07:05,970 --> 00:07:10,710 That's check expects as examples. And the more it's worth having good 75 00:07:10,710 --> 00:07:14,160 tests. That's check expect as tests. 76 00:07:14,160 --> 00:07:16,280 So that if we don't get this right, [SOUND] we'll know about it. 77 00:07:16,280 --> 00:07:28,120 Put it this way [SOUND]. If you've got next boards wrong, then 78 00:07:28,120 --> 00:07:30,645 there is no way solve is going to work, right. 79 00:07:30,645 --> 00:07:36,160 And it's going to be hard debugging solve if the wheel problem is in next boards. 80 00:07:36,160 --> 00:07:39,000 So even those these tests were a bit cumbersome to make, they were worth 81 00:07:39,000 --> 00:07:43,000 making. [SOUND] And I think actually that's 82 00:07:43,000 --> 00:07:47,410 going to be enough in this case. I could make some more complicated cases, 83 00:07:47,410 --> 00:07:50,609 but I think we'll be able to test the helpers to do that. 84 00:07:52,000 --> 00:07:56,760 And the reason I think that is look at this purpose. 85 00:07:56,760 --> 00:08:03,279 We find the first empty square we fill that with natural one to nine. 86 00:08:04,570 --> 00:08:16,450 And we keep only the valid boards. How are we going to template this? 87 00:08:16,450 --> 00:08:19,450 To me, it templates as a function composition. 88 00:08:19,450 --> 00:08:23,060 Starting with the board, we're going to do three distinct things. 89 00:08:23,060 --> 00:08:27,330 Three transformations of what we get to get the result. 90 00:08:27,330 --> 00:08:32,160 So I'm going to template this as define next boards of board. 91 00:08:32,160 --> 00:08:43,600 You can template it this way. I'm going to start with find first blank 92 00:08:43,600 --> 00:08:50,170 of board. And once I've got that blank, I'm 93 00:08:50,170 --> 00:09:03,708 going to say, fill with 1 to 9, the blank spot on the board. 94 00:09:03,708 --> 00:09:08,580 So this 1st thing, find blank, gets me the blank spot. 95 00:09:08,580 --> 00:09:14,472 This next thing says, hey, in this board, fill the blank spot with 1 to 9. 96 00:09:14,472 --> 00:09:18,320 So this first thing produces a single pause. 97 00:09:18,320 --> 00:09:28,300 This second thing produces a list of 9 boards, and I'm going to say keep only 98 00:09:28,300 --> 00:09:34,952 valid of that. And that last thing produces up to 9 99 00:09:34,952 --> 00:09:39,510 boards. I'm templating it as a composition. 100 00:09:39,510 --> 00:09:43,390 To understand the function composition probably the best thing to do now is do 101 00:09:43,390 --> 00:09:50,730 the wishlist entries. So let's see, find blank, it consumes a 102 00:09:50,730 --> 00:09:53,460 board and it produces a pause or a position. 103 00:09:53,460 --> 00:10:05,870 And it produces the position of the first blank square. 104 00:10:05,870 --> 00:10:11,010 And one thing that makes this function's job a little easier is we're only 105 00:10:11,010 --> 00:10:13,480 going to call it on boards that we know aren't full. 106 00:10:15,600 --> 00:10:23,870 Because if you go back here, first we ask hey, is the board solved. 107 00:10:24,920 --> 00:10:28,565 And only if the board is not solved, in other words if it's not full do we call 108 00:10:28,565 --> 00:10:39,610 next-boards. So this function can assume, the board, 109 00:10:39,610 --> 00:10:48,426 has whoops, the board has at least one blank square. 110 00:10:48,426 --> 00:10:56,769 Define, oops, is a wish list entry. Define blank a board. 111 00:10:56,769 --> 00:11:05,882 And we need to produce a position, so let's just say zero stub. 112 00:11:05,882 --> 00:11:10,340 So that's our wish list entry for find blank. 113 00:11:10,340 --> 00:11:13,780 Here is our wish list entry for fill, with 1 to 9. 114 00:11:13,780 --> 00:11:17,900 Let's see. It's 1st argument is going to be a Pos, 115 00:11:17,900 --> 00:11:21,960 and it's 2nd argument is going to be a Board, and it's going to produce, well, 116 00:11:21,960 --> 00:11:32,900 it has to produce a list of Board. We want to take the Board and the blank. 117 00:11:32,900 --> 00:11:39,590 And produce one board for the filling of the blank with one, and a board for the 118 00:11:39,590 --> 00:11:42,160 filling of the blank with two. And a board for filling of the blank with 119 00:11:42,160 --> 00:11:53,603 three and so on dot dot dot. Produce nine boards with, blank filled 120 00:11:53,603 --> 00:12:04,253 with Natural, 1 to 9, it's a wish list entry. 121 00:12:11,990 --> 00:12:17,935 And finally keep only valid. Well, keep only valid consumes a list of 122 00:12:17,935 --> 00:12:27,480 board and it produces a list of board. Produce list containing only valid 123 00:12:27,480 --> 00:12:39,155 boards. It's a wishlist entry and there's the 124 00:12:39,155 --> 00:12:52,010 stub. Lobd is empty. 125 00:12:52,010 --> 00:12:55,890 I didn't mark this as a stub, although it obviously was one, and I'm going to mark 126 00:12:55,890 --> 00:13:00,210 this as a stub. See if we run we'll see if all the stubs 127 00:13:00,210 --> 00:13:04,880 and things are well formed. What happened there? 128 00:13:04,880 --> 00:13:10,065 Oh I forgot to comment out the stub for next-boards. 129 00:13:10,065 --> 00:13:13,914 Fill with 1 to 9 expects only one argument. 130 00:13:13,914 --> 00:13:19,106 Oh, yeah I forgot to give fill, I, I put two in the signature but I only put one 131 00:13:19,106 --> 00:13:22,802 in the stub. So, we'll say, let's see which argument 132 00:13:22,802 --> 00:13:26,410 comes first. The position comes first and then the 133 00:13:26,410 --> 00:13:28,820 board. So let's see we'll just call the position 134 00:13:28,820 --> 00:13:32,730 parameter P. Great. 135 00:13:32,730 --> 00:13:37,280 Now, we've got some failing tests, which isn't so surprising, but things seem to 136 00:13:37,280 --> 00:13:41,210 be well formed. You know, it's kind of one step forward, 137 00:13:41,210 --> 00:13:45,840 two steps back here. We had a wishlist entry for solved, and 138 00:13:45,840 --> 00:13:50,106 we were able to finish solved, so that was great, but we also had a wishlist 139 00:13:50,106 --> 00:13:55,080 entry for Nextboards In when we design next boards, we ended up with three more 140 00:13:55,080 --> 00:13:59,965 wish list entries. But some point they start actually 141 00:13:59,965 --> 00:14:05,653 resolving.