changeset 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 c9873b78e8c2
children f86bb0d669be
files 3-Prolog/day2-unification.pl 3-Prolog/readme.txt
diffstat 2 files changed, 28 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/3-Prolog/day2-unification.pl	Tue Sep 26 20:58:49 2017 +0100
@@ -0,0 +1,17 @@
+% Unification works both sides of the equals:
+% (A, 2, C) = (1, B, 3).
+% We can't actually run this from a file, because Prolog. We get:
+%
+%     native code procedure (=)/2 cannot be redefined (ignored)
+%
+% But we can run it from the REPL.
+%
+% Lists have heads and tails:
+%
+% [a, b, c] = [Head|Tail].
+%
+% Underscore is the wildcard variable:
+%
+% [a, b, c] = [_|Tail].
+%
+% Note: It's not magic "Head|Tail", it's "[X|Y]" notation
\ No newline at end of file
--- a/3-Prolog/readme.txt	Tue Sep 26 20:47:20 2017 +0100
+++ b/3-Prolog/readme.txt	Tue Sep 26 20:58:49 2017 +0100
@@ -11,4 +11,14 @@
 When you ask a question with a placeholder, the placeholder has to be capitalised. Lower-case makes it an atom.
 When Prolog returns its answers, type ";" to get the next response or "a" to get all. Sometimes Prolog will end multiple responses
 with "no", sometimes with "yes". This depends on whether it needs to do more calculations to check for more answers ("no")
-or whether it knows it successfully gave you everything ("yes").
\ No newline at end of file
+or whether it knows it successfully gave you everything ("yes").
+
+Rules don't need to be defined once. If you define the following:
+
+ancestor(X, Y) :- father(X, Y).
+ancestor(X, Y) :- father(X, Z), ancestor(Z, Y).
+
+then Prolog will check both "definitions" of the rule.
+
+(Note: putting the recursive "ancestor/2" call at the end is called "tail recursion" and the language can optimise it to
+reduce call stack exhaustion issues)
\ No newline at end of file