|
113 | 113 | <code>def</code> binds the newly defined function to a name. |
114 | 114 | </p> |
115 | 115 | <p> |
116 | | - If you want, you can create a named functions without |
| 116 | + If you want, you can create a named function without |
117 | 117 | using <code>defn</code>; type |
118 | 118 | <pre class="codeblock"> |
119 | 119 | <code class="expr"> |
|
150 | 150 | elements, you actually get a brand new list. (Fortunately, |
151 | 151 | Clojure is amazingly efficient at creating new lists). In |
152 | 152 | general, Clojure encourages you to have as little mutable state |
153 | | - as possible. For example, instead qof "for" loops and other |
| 153 | + as possible. For example, instead of "for" loops and other |
154 | 154 | state-changing constructs, most of the time you'll see functions |
155 | 155 | doing transformations on immutable data and returning new |
156 | 156 | collections, without changing the old one. |
|
601 | 601 | <div class="page" data-exitexpr="true"> |
602 | 602 | <p> |
603 | 603 | Before we go on to solve the puzzle, let's write a function to |
604 | | - pretty-print our grid. We'd like the output to look like: |
| 604 | + pretty-print our grid. We'd like the output to look like this: |
605 | 605 | </p> |
606 | 606 | <pre> |
607 | 607 | +---+---+---+ |
|
1168 | 1168 | Hmm. In this case, <code>eliminate-one</code> is returning <code>nil</code> when |
1169 | 1169 | we try to eliminate 5 as a value for A1, and <code>reduce</code> carries on looping |
1170 | 1170 | with a <code>nil</code> accumulator. We need a way to short-circuit the reduce |
1171 | | - when we run into a <code>nil</code> value: |
| 1171 | + when we run into a <code>nil</code> value. Try this new version |
| 1172 | + of <code>eliminate</code>: |
1172 | 1173 | </p> |
1173 | 1174 | <pre class="codeblock"> |
1174 | 1175 | <code class="expr"> |
|
1356 | 1357 | </code> |
1357 | 1358 | </pre> |
1358 | 1359 | <p> |
1359 | | - Try solving this grid: <code class="expr">(render-grid (solve hard-grid))</code>. |
| 1360 | + Try solving this grid: |
| 1361 | + <code class="expr">(render-grid (solve hard-grid))</code>. |
| 1362 | + (Again, it may take a few seconds.) |
1360 | 1363 | </p> |
1361 | 1364 | </div> |
1362 | 1365 | <div class="page" data-exitexpr='(find-unsolved hard-grid)'> |
|
1371 | 1374 | <p> |
1372 | 1375 | This is where Clojure's immutable data structures really show their strength: |
1373 | 1376 | backtracking comes for free, as trying a value creates a new grid without mutating |
1374 | | - the original one! Let's take a look at our <code>solve</code> function: |
| 1377 | + the original one! Let's take a look at our <code>solve</code> |
| 1378 | + function so far: |
1375 | 1379 | </p> |
1376 | 1380 | <pre class="codeblock"> |
1377 | 1381 | <code class="expr"> |
|
0 commit comments