% 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]). 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,a,b,c,d,[1,2],[1,2,3]]), \+ p(X). q(X). % singleton variable /* ?- t(X). % nu va găsi soluții singur. false. ?- t1(X). ?- t1(X). X = 3 ; X = 4 ; X = 5 ; X = b ; X = c ; X = d ; X = [1, 2] ; false. ?- q(1). true. ?- q(X). true. ?- [1, X, Y] = [Z, 2, T]. X = 2, Y = T, Z = 1. ?- Lista = [1, X, Y], Lista = [Z, 2, T], Y = 3. Lista = [1, 2, 3], X = 2, Y = T, T = 3, Z = 1. ?- Lista = [1, X, Y], Lista = [Z, 2, T], Y = 3, T = 4. false. */ % 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]). saysLiedYest(Animal, Today) :- next(Yest, Today), lies(Animal, DaysLies), % print(DaysLies), \+ member(Today, DaysLies), member(Yest, DaysLies). saysLiedYest(Animal, Today) :- next(Yest, Today), lies(Animal, Days), member(Today, Days), \+ member(Yest, Days). sol(Today) :- saysLiedYest(lion, Today), saysLiedYest(unicorn, Today). % predicatul doubleP este adevărat dacă ambele argumente ale lui sunt % același lucru și dacă p este adevărat doubleP(X, X) :- p(X). /* ?- [1,2,3,4] = [X | Tail]. X = 1, Tail = [2, 3, 4]. ?- [1,2,3,4] = [X,Y,Z | Tail]. X = 1, Y = 2, Z = 3, Tail = [4]. */ % myMember/2 % myMember(Elem, Lista) %myMember(_, []) :- false. % nu este necesar myMember(E, [E | _]). % elementul este primul element din lista %myMember(E, [X | L]) :- X = E. % putem folosi =, dar nu e nevoie myMember(E, [_ | T]) :- myMember(E, T). % elementul nu este în restul listei. % sumList/2 % sumList(Lista, Suma) sumList([], 0). sumList([H | T], Sum) :- sumList(T, SumTail), Sum is H + SumTail. % incList/2 % incList(ListIn, ListOut) incList([], []). incList(LI, LO) :- LI = [H | T], LO = [IncH | IncTail], incList(T, IncTail), %IncH is H + 1. plus(H, 1 ,IncH). /* ?- incList([1,2,3], L). L = [2, 3, 4]. % merge doar dacă folosesc plus/3, nu is: ?- incList(L, [2,3,4]). L = [1, 2, 3] ; false. % directioalitate flexibilă a executiei: ?- append([1,2,3], [4,5,6], L). L = [1, 2, 3, 4, 5, 6]. ?- append([1,2,3], L, [1,2,3,4,54,6]). L = [4, 54, 6]. ?- append([1,X, Y], [Z,T,4], [1,2,3,4,54,6]). false. ?- append([1,X, Y], [Z,T,4], [1,2,3,4,54,4]). X = 2, Y = 3, Z = 4, T = 54. ?- append(L1, L2, L3). L1 = [], L2 = L3 ; L1 = [_A], L3 = [_A|L2] ; L1 = [_A, _B], L3 = [_A, _B|L2] ; L1 = [_A, _B, _C], */