% 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 p(1). p(2). p(a). p([1,2,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]). is_next(Y, T, [Y, T|_]). is_next(Y, T, [_|Days]) :- is_next(Y, T, Days). yesterday(T, Y) :- days(Days), (is_next(Y, T, Days) ; Days = [T|_], reverse(Days, [Y|_])). 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). % 0. o funcție simplă pe liste % ========================================== % myMember(+Elem, +Lista) myMember(_, []) :- fail. % lista vida nu poate contine elementul. Aceasta linie poate sa lipseasca. % myMember(E, [H|_]) :- E == H. myMember(E, [E|_]). % elementul unifică cu primul element din lista myMember(E, [_|T]) :- myMember(E, T). % elementul nu este primul din lista. myMember2(X, [H|T]) :- X=H ; myMember2(X, T). % folosind 'sau' % 1. variabile neinstanțiate % ========================================== % any(X). any(X) :- display(X). % sau: any(X) :- format("X este: ~w~n", [X]). any(X, X). % aceeași variabilă în mai multe locuri => unificare % încercați: % ?- any(X, Y). % any(X, Y) :- X = Y. % identic cu any(X, X). % any(X, Y) :- X is Y. % încercați any(X, 1+2) cu ambele variante de implementare de mai sus. % la consolă vad 'ce legări s-au forțat ca să fie adevarat?'. /* de executat la consolă: ?- any(X,Y), X = 5. X = Y, Y = 5. ?- any(X,Y), Y = 5. X = Y, Y = 5. ?- [1,2,3] = [1,2,3]. true. ?- [1,X,Y] = [1,2,3]. X = 2, Y = 3. ?- [1,X,Y] = [Z,2,3]. X = 2, Y = 3, Z = 1. ?- [1,X,Y] = Z. Z = [1, X, Y]. ?- [1,X,Y] = Z, (X,Y)=(2,3). X = 2, Y = 3, Z = [1, 2, 3]. ?- */ % 3. liste; direcția construirii soluțiilor % ========================================== % o prelucrare simplă pe liste % sumList(+L, -Sum) sumList([], 0). sumList([H|T], Sum) :- sumList(T, SumTail), Sum is SumTail + H. % doresc ceva echivalent cu map (+1) list % Simplu - recursivitate pe stivă (se mai face o unificare la % întoarcere, în argumentul de ieșire). % incList(+LI, -LO) incList([], []). /* incList(LI, LO) :- LI = [First|Rest], IncFirst is First + 1, incList(Rest, IncRest), LO = [IncFirst|IncRest]. */ % mai simplu: incList([F|R], [IF|IR]) :- IF is F + 1, incList(R, IR). % Cu acumulator și construcția rezultatului pe avansul în recursivitate. % Recursivitate pe coada % incListTailA(+LI, +Acc, -LO) incListTailA([], Acc, Acc). incListTailA([F|R], Acc, Res) :- IF is F + 1, incListTailA(R, [IF|Acc], Res). incListTail(LI, LO) :- incListTailA(LI, [], R), reverse(R, LO). /* ?- append([1,2,3], [2,3,4], L). L = [1, 2, 3, 2, 3, 4]. ?- append([1,2,3], L, [1,2,3,4,5]). L = [4, 5]. ?- 1 < 2. true. ?- abc < adb. ERROR: Arithmetic: `abc' is not a function ERROR: In: ... ?- abc @< adb. true. ?- [1,2,3] @< [1,3,4]. true. ?- [1,2,3] @< [1,2,2]. false. ?- member(X, [1,2,3,4,5]), X > 2. X = 3 ; X = 4 ; X = 5. */ % findall(template = ce pun în listă, scop, listă cu acele legări % pentru template care satisfac scopul) /* ?- findall(X, (member(X, [1,2,3,4,5]), X > 2), Bag). Bag = [3, 4, 5]. ?- findall((X, Y), (member(X, [1,2,3,4,5]), X > 2, member(Y, [a, b, c])), Bag). Bag = [(3, a), (3, b), (3, c), (4, a), (4, b), (4, c), (5, a), (5, b), (..., ...)]. ?- findall((X, Y), (member(X, [1,2,3,4,5]), X > 3, member(Y, [a, b, c])), Bag). Bag = [(4, a), (4, b), (4, c), (5, a), (5, b), (5, c)]. ?- */