annotate 3-Prolog/readme.txt @ 58:90c4b7f28690

Add initial Day 2 notes for Prolog
author IBBoard <dev@ibboard.co.uk>
date Tue, 26 Sep 2017 20:58:49 +0100
parents 005ae3fad18f
children 05871e7ac124
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
51
178b18b4f9ba Start experimenting with Prolog
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 Installed gprolog from https://software.opensuse.org/package/gprolog
178b18b4f9ba Start experimenting with Prolog
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2
178b18b4f9ba Start experimenting with Prolog
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 Some documentation says that Prolog may accept a shebang as the first line, but gprolog didn't like it.
178b18b4f9ba Start experimenting with Prolog
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
178b18b4f9ba Start experimenting with Prolog
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 Some editors will assume ".pl" means Perl. You may need to change your syntax highlighting.
178b18b4f9ba Start experimenting with Prolog
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6
178b18b4f9ba Start experimenting with Prolog
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 Running code involves calling "gprolog" without parameters and then running "['filename']." (fullstop terminates the commend).
178b18b4f9ba Start experimenting with Prolog
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 You can then query it with function calls (e.g. "likes(grommit, sheep)." in the Wallace & Grommit example). Prolog always
178b18b4f9ba Start experimenting with Prolog
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 returns "yes" or "no" at a prompt when it has finished.
178b18b4f9ba Start experimenting with Prolog
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10
178b18b4f9ba Start experimenting with Prolog
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 When you ask a question with a placeholder, the placeholder has to be capitalised. Lower-case makes it an atom.
53
005ae3fad18f Write up the map colouring code
IBBoard <dev@ibboard.co.uk>
parents: 51
diff changeset
12 When Prolog returns its answers, type ";" to get the next response or "a" to get all. Sometimes Prolog will end multiple responses
51
178b18b4f9ba Start experimenting with Prolog
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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
90c4b7f28690 Add initial Day 2 notes for Prolog
IBBoard <dev@ibboard.co.uk>
parents: 53
diff changeset
24 reduce call stack exhaustion issues)