Skip to content

Instantly share code, notes, and snippets.

@retraigo
Last active May 16, 2023 04:16
Show Gist options
  • Save retraigo/1041b979f092181fa4387aec1104fe75 to your computer and use it in GitHub Desktop.
Save retraigo/1041b979f092181fa4387aec1104fe75 to your computer and use it in GitHub Desktop.
in_room(monkey).
in_room(banana).
in_room(chair).
clever(monkey).
can_climb(monkey, chair).
tall(chair).
can_move(monkey, chair, banana).
can_reach(X, Y):-
under(Z, Y),
tall(Z),
can_climb(X, Z),
clever(X).
under(X, Y):-
can_move(Z, X, Y),
in_room(X),
in_room(Y),
in_room(Z),
clever(Z).
symptom(charlie,fever).
symptom(charlie,headache).
symptom(charlie,runnynose).
symptom(charlie,rash).
hypothesis(Patient,measles) :-
symptom(Patient,fever),
symptom(Patient,cough),
symptom(Patient,conjunctive),
symptom(Patient,runnynose),
symptom(Patient,rash).
hypothesis(Patient,germanmeasles):-
symptom(Patient,fever),
symptom(Patient,headache),
symptom(Patient,runnynose),
symptom(Patient,rash).
hypothesis(Patient,flu):-
symptom(Patient,fever),
symptom(Patient,headache),
symptom(Patient,bodyache),
symptom(Patient,chills),
symptom(Patient,sorethrought),
symptom(Patient,cough),
symptom(Patient,conjunctive),
symptom(Patient,conjunctive),
symptom(Patient,runnynose).
hypothesis(Patient,commoncold):-
symptom(Patient,headache),
symptom(Patient,runnynose),
symptom(Patient,snuzing),
symptom(Patient,chills),
symptom(Patient,sorethrought).
hypothesis(Patient,mumps):-
symptom(Patient,fever),
symptom(Patient,swallenglands).
hypothesis(Patient,chikenpox):-
symptom(Patient,fever),
symptom(Patient,rash),
symptom(Patient,bodyache).
hypothesis(Patient,whooping-cough):-
symptom(Patient,runnynose),
symptom(Patient,snuzing),
symptom(Patient,cough).
factorial(0, 1).
factorial(X, Result):-
X > 0,
X1 is X - 1,
factorial(X1, R1),
Result is X * R1.
fibonacci(N, Y):-
N > 0,
fibo_series(N, Y, _).
fibo_series(1, 1, 0).
fibo_series(N, Y, Y1):-
N1 is N - 1,
fibo_series(N1, Y1, Y2),
Y is Y1 + Y2.
use_module(library(clpfd)).
n_queens(N, Qs) :-
length(Qs, N),
Qs ins 1..N,
safe_queens(Qs).
safe_queens([]).
safe_queens([Q|Qs]) :- safe_queens(Qs, Q, 1), safe_queens(Qs).
safe_queens([], _, _).
safe_queens([Q|Qs], Q0, D0) :-
Q0 #\= Q,
abs(Q0 - Q) #\= D0,
D1 #= D0 + 1,
safe_queens(Qs, Q0, D1).
% n_queens(8, Qs), label(Qs).
road(1,2,50).
road(1,3,100).
road(1,4,75).
road(1,5,65).
road(2,5,30).
road(3,4,40).
road(3,7,50).
road(4,5,25).
road(4,6,30).
road(4,9,65).
road(4,7,70).
road(5,6,30).
road(6,9,35).
route(Town1, Town2, Distance):-
road(Town1, Town2, Distance).
route(Town1, Town2, Distance):-
road(Town1, X, Dist1),
route(X, Town2, Dist2),
Distance is Dist1 + Dist2.
solve(A,B):- %Input packaging
moves([s(A,B)],RevStates), %add list
reverse(RevStates,States), %reverse answers
write(States),nl,fail. %write and find all answer.
moves([s(X0,Y0)|T], [s(2,Y1),s(X0,Y0)|T]) %set goal
:- action(s(X0,Y0),s(2,Y1)), !. %cut off when find answer
moves([s(X0,Y0)|T],States) :-
action(s(X0,Y0),s(X1,Y1)), %find next node
not(member(s(X1,Y1),[s(X0,Y0)|T])), %not look for past nodes
moves([s(X1,Y1),s(X0,Y0)|T],States). %repeat moves
action(s(X,Y),s(Z,3)) :- %from left jug to right jug until right full
X=<4,X>0,Y>=0,Y<3,Z is X - (3 - Y), Z >= 0.
action(s(X,Y),s(Z,0)) :- %from right jug to left jug until right empty
X<4,X>=0,Y>0,Y=<3,Z is X + Y, Z =< 4.
action(s(X,Y),s(4,Z)) :- %from right jug to left jug until left full
X<4,X>=0,Y>0,Y=<3,Z is Y - (4 - X), Z >=0.
action(s(X,Y),s(0,Z)) :- %from left jug to right jug until left empty
X=<4,X>0,Y>=0,Y<3,Z is X + Y, Z =< 3.
action(s(X,Y),s(4,Y)):- %fill left jug (4)
X<4,X>=0,Y>=0,Y=<3.
action(s(X,Y),s(X,3)):- %fill right jug (3)
X=<4,X>=0,Y>=0,Y<3.
action(s(X,Y),s(X,0)) :- %empty right jug
X=<4,X>=0,Y>0,Y=<3.
action(s(X,Y),s(0,Y)) :- %empty left jug
X=<4,X>0,Y>=0,Y=<3.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment