This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% set_accum/3 - A helper function using an acculumator to form a set from a given list | |
set_accum([H | T], Acc, Result) :- member(H, Acc), set_accum(T, Acc, Result). | |
set_accum([H | T], Acc, Result) :- \+ member(H, Acc), set_accum(T, [H | Acc], Result). | |
set_accum([], Acc, Acc). | |
% set/2 - Create a set of elements from a list | |
set(InList, OutList) :- set_accum(InList, [], Intermediate), reverse(Intermediate, OutList). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
member(X, List) :- append(_, [X | _], List). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Learn Prolog Now! - Chapter 6 - Exercise 6.6 - Who owns the zebra? | |
% A simplified version of Einstein's Riddle | |
% Problem Statement: | |
% There is a street with three neighboring houses that all have a different color, namely red, blue and green. People of different nationalities live in the different houses and they all have a different pet. Here are some more facts about them: | |
% - The Englishman lives in the red house | |
% - The jaguar is the pet of the Spanish family | |
% - The Japanese lives to the right of the snail keeper | |
% - The snail keeper lives to the left of the blue house | |
% Who keeps the zebra? zebra/1 should tell us who this person is. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
swapfl(List1, List2) :- append([A | M], [B], List1), append([B | M], [A], List2). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
last([X], X). | |
last([_ | T], X) :- last(T, X). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
last(List, X) :- reverse([X | _], List). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
toptail([_ | T], OutList) :- append(OutList, [_], T). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
palindrome(Result) :- | |
append(L0, L1, Result), reverse(L0, L1); | |
append(L0, [_ | L1], Result), reverse(L0, L1). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
doubled(List) :- append(L, L, List). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dot([], [], 0). | |
dot([X | V1], [Y | V2], Z) :- dot(V1, V2, Result), Z is X * Y + Result. |