% 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]). %always(X). % singleton variable always(_). t(X) :- \+ p(X). % t este adevărat pentru X dacă nu se poate demonstra p(X) % putem pune predicatul să găsească soluții dacă avem un domeniu. t1(X) :- member(X, [1,2,3,4,5,6,a,b,c,[1,2]]), \+ p(X). % predicatul same este adevărat dacă ambele argumente sunt același lucru same(X, X). /* Unificări ?- [X, 2, Z] = [1,Y,3]. X = 1, Z = 3, Y = 2. ?- [X, 2, Z] = [Y,1,3]. false. ?- [X, 2, Z] = [Y,2,3]. X = Y, Z = 3. ?- [X, 2, Z] = [Y,2,3], writeln(X), Y = 2, writeln(X). _22552 2 X = Y, Y = 2, Z = 3. */ % 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? days([mon, tue, wed, thu, fri, sat, sun]). check_next(Y, T, [Y, T|_]). check_next(Y, T, [_|Days]) :- check_next(Y, T, Days). next(Y, T) :- days([T|_]), days(Days), reverse(Days, [Y|_]). next(Y, T) :- days(Days), check_next(Y, T, Days). lies(lion, [mon, tue, wed]). lies(unicorn, [thu, fri, sat]). saysLiedYesterday(Animal, Today) :- lies(Animal, DaysLies), \+ member(Today, DaysLies), next(Yesterday, Today), member(Yesterday, DaysLies). saysLiedYesterday(Animal, Today) :- lies(Animal, DaysLies), member(Today, DaysLies), next(Yesterday, Today), \+ member(Yesterday, DaysLies). solution(Today) :- days(Days), member(Today, Days), saysLiedYesterday(lion, Today), saysLiedYesterday(unicorn, Today). /* Prelucrare liste ?- [1,2,3] = [H|T]. H = 1, T = [2, 3]. ?- [1,2,3,4,5] = [H1,H2,H3|T]. H1 = 1, H2 = 2, H3 = 3, T = [4, 5]. */ % myMember/2 % myMember(?E, ?L) myMember(_, []) :- false. % nu este necesar myMember(E, L) :- L = [H | _], E = H. % elementul este primul element din lista myMember(E, L) :- L = [_ | T], myMember(E, T). % elementul este în restul listei. myMember2(E, [E | _]). myMember2(E, [_ | T]) :- myMember2(E, T). /* Aspect generativ ?- member(1, [1,2,3]). true . ?- member(X, [1,2,3]). X = 1 ; X = 2 ; X = 3. ?- append([1,2,3], [4,5,6], A). A = [1, 2, 3, 4, 5, 6]. ?- append([1,2,3], X, [1,2,3,4,5,6]). X = [4, 5, 6]. ?- append(X, Y, [1,2,3,4,5,6]). X = [], Y = [1, 2, 3, 4, 5, 6] ; X = [1], Y = [2, 3, 4, 5, 6] ; X = [1, 2], Y = [3, 4, 5, 6] ; X = [1, 2, 3], Y = [4, 5, 6] ; X = [1, 2, 3, 4], Y = [5, 6] ; X = [1, 2, 3, 4, 5], Y = [6] ; X = [1, 2, 3, 4, 5, 6], Y = [] ; false. */ % sumList/2 % sumList(+List, -Sum) sumList([], 0). %sumList([H|T], Sum) :- sumList(T, SumTail), Sum is H + SumTail. sumList([H|T], H + SumTail) :- sumList(T, SumTail). /* operatori de egalitate ?- X = Y. X = Y. ?- X == Y. false. ?- X = Y, X == Y. X = Y. ?- X \= Y. false. ?- X = 1+2. X = 1+2. ?- X is 1+2. X = 3. ?- X = 1+2, Y = X+5, Z = Y / 4. X = 1+2, Y = 1+2+5, Z = (1+2+5)/4. ?- X = 1+2, Y = X+5, Z = Y / 4, T is Z. X = 1+2, Y = 1+2+5, Z = (1+2+5)/4, T = 2. ?- 1+2=X+Y. X = 1, Y = 2. ?- 1+2 = 2+1. false. */ % incList/2 % incList(+LIn, -LOut) incList([], []). incList([HIn | TIn], [HOut | TOut]) :- %HOut is HIn + 1, plus(HIn, 1, HOut), incList(TIn, TOut).