Playing around with prolog
authorDavid Kerkeslager <kerkeslager@gmail.com>
Tue, 13 Dec 2016 14:46:53 +0000 (09:46 -0500)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Tue, 13 Dec 2016 14:46:53 +0000 (09:46 -0500)
prolog/test.pl [new file with mode: 0644]

diff --git a/prolog/test.pl b/prolog/test.pl
new file mode 100644 (file)
index 0000000..388639d
--- /dev/null
@@ -0,0 +1,20 @@
+bitor(1, [1|_]).
+bitor(0, [0]).
+bitor(B, [0|T]) :- bitor(B, T).
+
+bitand(0, [0|_]).
+bitand(1, [1]).
+bitand(B, [1|T]) :- bitand(B, T).
+
+bitcomposition(0, []).
+bitcomposition(N, [H|T]) :-
+    H is N mod 2,
+    Shift is N // 2,
+    bitcomposition(Shift, T).
+
+
+inc(N, NPlusOne) :- add(NPlusOne, N, 1).
+
+add(Z, X, Y) :- var(X), X is Z - Y.
+add(Z, X, Y) :- var(Y), Y is Z - X.
+add(Z, X, Y) :- Z is X + Y.