Mercurial > repos > other > SevenLanguagesInSevenWeeks
annotate 3-Prolog/readme.txt @ 69:05871e7ac124
Add notes on how to invoke the Sudoku code
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 07 Oct 2017 16:50:14 +0100 |
parents | 90c4b7f28690 |
children |
rev | line source |
---|---|
51 | 1 Installed gprolog from https://software.opensuse.org/package/gprolog |
2 | |
3 Some documentation says that Prolog may accept a shebang as the first line, but gprolog didn't like it. | |
4 | |
5 Some editors will assume ".pl" means Perl. You may need to change your syntax highlighting. | |
6 | |
7 Running code involves calling "gprolog" without parameters and then running "['filename']." (fullstop terminates the commend). | |
8 You can then query it with function calls (e.g. "likes(grommit, sheep)." in the Wallace & Grommit example). Prolog always | |
9 returns "yes" or "no" at a prompt when it has finished. | |
10 | |
11 When you ask a question with a placeholder, the placeholder has to be capitalised. Lower-case makes it an atom. | |
53 | 12 When Prolog returns its answers, type ";" to get the next response or "a" to get all. Sometimes Prolog will end multiple responses |
51 | 13 with "no", sometimes with "yes". This depends on whether it needs to do more calculations to check for more answers ("no") |
58
90c4b7f28690
Add initial Day 2 notes for Prolog
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
14 or whether it knows it successfully gave you everything ("yes"). |
90c4b7f28690
Add initial Day 2 notes for Prolog
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
15 |
90c4b7f28690
Add initial Day 2 notes for Prolog
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
16 Rules don't need to be defined once. If you define the following: |
90c4b7f28690
Add initial Day 2 notes for Prolog
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
17 |
90c4b7f28690
Add initial Day 2 notes for Prolog
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
18 ancestor(X, Y) :- father(X, Y). |
90c4b7f28690
Add initial Day 2 notes for Prolog
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
19 ancestor(X, Y) :- father(X, Z), ancestor(Z, Y). |
90c4b7f28690
Add initial Day 2 notes for Prolog
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
20 |
90c4b7f28690
Add initial Day 2 notes for Prolog
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
21 then Prolog will check both "definitions" of the rule. |
90c4b7f28690
Add initial Day 2 notes for Prolog
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
22 |
90c4b7f28690
Add initial Day 2 notes for Prolog
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
23 (Note: putting the recursive "ancestor/2" call at the end is called "tail recursion" and the language can optimise it to |
69
05871e7ac124
Add notes on how to invoke the Sudoku code
IBBoard <dev@ibboard.co.uk>
parents:
58
diff
changeset
|
24 reduce call stack exhaustion issues) |
05871e7ac124
Add notes on how to invoke the Sudoku code
IBBoard <dev@ibboard.co.uk>
parents:
58
diff
changeset
|
25 |
05871e7ac124
Add notes on how to invoke the Sudoku code
IBBoard <dev@ibboard.co.uk>
parents:
58
diff
changeset
|
26 For the sudoku code, example invocations are: |
05871e7ac124
Add notes on how to invoke the Sudoku code
IBBoard <dev@ibboard.co.uk>
parents:
58
diff
changeset
|
27 |
05871e7ac124
Add notes on how to invoke the Sudoku code
IBBoard <dev@ibboard.co.uk>
parents:
58
diff
changeset
|
28 % 2×2 grid, from the book - solved correctly |
05871e7ac124
Add notes on how to invoke the Sudoku code
IBBoard <dev@ibboard.co.uk>
parents:
58
diff
changeset
|
29 sudoku([_,_,2,3, _,_,_,_, _,_,_,_, 3,4,_,_], 2,2, Solution). |
05871e7ac124
Add notes on how to invoke the Sudoku code
IBBoard <dev@ibboard.co.uk>
parents:
58
diff
changeset
|
30 |
05871e7ac124
Add notes on how to invoke the Sudoku code
IBBoard <dev@ibboard.co.uk>
parents:
58
diff
changeset
|
31 % 2x3 grid (size suggested in the exercise) |
05871e7ac124
Add notes on how to invoke the Sudoku code
IBBoard <dev@ibboard.co.uk>
parents:
58
diff
changeset
|
32 % For some reason, this one can't be perfectly solved unless certain extra values are added |
05871e7ac124
Add notes on how to invoke the Sudoku code
IBBoard <dev@ibboard.co.uk>
parents:
58
diff
changeset
|
33 sudoku([_,6,_,_,_,_, 1,_,3,_,5,_, _,_,1,_,_,2, 6,_,_,4,_,_, _,5,_,3,_,1, _,_,_,_,2,_], 3, 2, Solution). |
05871e7ac124
Add notes on how to invoke the Sudoku code
IBBoard <dev@ibboard.co.uk>
parents:
58
diff
changeset
|
34 |
05871e7ac124
Add notes on how to invoke the Sudoku code
IBBoard <dev@ibboard.co.uk>
parents:
58
diff
changeset
|
35 % Normal 3x3 grid |
05871e7ac124
Add notes on how to invoke the Sudoku code
IBBoard <dev@ibboard.co.uk>
parents:
58
diff
changeset
|
36 sudoku([_,8,3,_,4,_,_,1,7, 6,_,_,7,_,_,_,4,_, _,_,_,8,_,_,9,_,_, 8,_,5,3,7,_,_,_,_, _,_,_,1,_,9,_,_,_, _,_,_,_,8,5,3,_,9, _,_,6,_,_,8,_,_,_, _,2,_,_,_,1,_,_,3, 3,4,_,_,2,_,5,9,_ ], 3, 3, Solution). |