% Manual Prolog % http://www.swi-prolog.org/pldoc/doc_for?object=manual % http://www.swi-prolog.org/pldoc/man?section=builtin % programul: descrie ce este adevărat, eventual în funcție de condiții. p(1). p(2). p(a). p([1,2,3]). q(X, Y) :- p(X), p(Y). % predicatul r este adevărat dacă ambele argumente ale lui sunt același lucru și dacă p(argument al lui q) este adevărat r(X, X) :- p(X). %t(X) :- \+ p(X). % nu va găsi soluții singur. % putem pune predicatul să găsească soluții dacă avem un domeniu. t(X) :- member(X, [1,2,3,4,5,a,b,c,d]), \+ p(X). % Alice came across a lion and a unicorn in a forest of forgetfulness. % Those two are strange beings. % The lion lies every Monday, Tuesday and Wednesday % and the other days he speaks the truth. % The unicorn lies on Thursdays, Fridays and Saturdays, % however the other days of the week he speaks the truth. % Lion: Yesterday I was lying. % Unicorn: So was I. % Which day did they say that? yesterday(mon, sun). yesterday(tue, mon). yesterday(wed, tue). yesterday(thu, wed). yesterday(fri, thu). yesterday(sat, fri). yesterday(sun, sat). lies(lion, [mon, tue, wed]). lies(unicorn, [thu, fri, sat]). % Animal spune azi (Today) ca ieri a mintit saysItLiedYesterday(Animal, Today) :- yesterday(Today, Yesterday), lies(Animal, DaysLies), % obtine DaysLies \+ member(Today, DaysLies), % azi minte member(Yesterday, DaysLies). saysItLiedYesterday(Animal, Today) :- yesterday(Today, Yesterday), lies(Animal, DaysLies), member(Today, DaysLies), \+ member(Yesterday, DaysLies). % solutia sol(Today) :- saysItLiedYesterday(lion, Today), saysItLiedYesterday(unicorn, Today). % mem/2 % mem(+E, +L) mem(_, []) :- false. % lista vida nu poate contine elementul. Aceasta linie poate sa lipseasca. mem(E, [H | _]) :- E = H. % elementul este primul element din lista mem(E, [H | T]) :- E \= H, mem(E, T). % elementul nu este primul din lista. mem2(E, [E | _]). mem2(E, [_ | T]) :- mem2(E, T). mem3(E, [H|T]) :- E = H; mem3(E, T). % sumList/2 % sumList(+L, -Sum) sumList([], 0). sumList([H|T], Sum) :- sumList(T, SumTail), Sum is H + SumTail. % map (+1) incList(L, LInc) :- L = [], LInc = []. incList(L, LInc) :- L = [H | T], LInc = [HInc | TInc], HInc is H + 1, incList(T, TInc). incList2([], []). incList2([H | T], [HInc | TInc]) :- HInc is H + 1, incList2(T, TInc).