%% fapte simple om(gica). om(ilie). impiedicat(gica). traverseaza(ilie, santier). %% ilie și santier sunt constante, este un șantier anume sapa_groapa(ilie, gica). %% reguli cade_in_groapa(X) :- impiedicat(X), traverseaza(X, santier). cade_in_groapa(X) :- sapa_groapa(X, Y), X \= Y. %% negație bun(X) :- \+sapa_groapa(X,_). om_bun(X) :- om(X), \+sapa_groapa(X,_). om_bun_(X) :- \+sapa_groapa(X,_), om(X). %% structuri (obiecte cu componente) carte(titlu('Razboi si pace'), autor('Lev Tolstoi')). carte(titlu('Sonata Kreutzer'), autor('Lev Tolstoi')). carte(titlu('Magicianul'), autor('John Fowles')). revista(titlu('National Geographic'), anul(2019), luna('septembrie')). %% predicate de folosit în interogări in_biblioteca(X - Y) :- carte(titlu(X), autor(Y)). in_biblioteca(X - Y/Z) :- revista(titlu(X), anul(Z), luna(Y)). %% de definit titlu(X) :- carte(titlu(X),_) ; revista(titlu(X),_,_). titlu_struct(X) :- carte(X,_) ; revista(X,_,_). de_autor(Autor, Carte) :- carte(titlu(Carte), autor(Autor)). % PF: f(g(h(x))) % PL: h(X,Y), g(Y,Z), f(Z,Res) %% exemple pe liste /* (define (num->base n b) (if (zero? n) '() (append (num->base (quotient n b) b) (list (modulo n b))))) */ % num_to_base(+N, +B, -Res) num_to_base(0, _, []). num_to_base(N, B, Res) :- N > 0, QB is N div B, MB is N mod B, num_to_base(QB, B, Res1), append(Res1, [MB], Res). /* (define (all-palindromes? n Bases) (if (null? Bases) #t (and (palindrome? (num->base n (car Bases))) (all-palindromes? n (cdr Bases))))) */ % palindrome(+L) palindrome(L) :- reverse(L, L). % all_palindromes(+N, +Bases) all_palindromes(_, []). all_palindromes(N, [B|Bs]) :- num_to_base(N, B, NB), palindrome(NB), all_palindromes(N, Bs). /* (define (palindromic-bases n Bases) (cond ((null? Bases) '()) ((palindrome? (num->base n (car Bases))) (cons (car Bases) (palindromic-bases n (cdr Bases)))) (else (palindromic-bases n (cdr Bases))))) */ % palindromic_bases(+N, +Bases, -PBases) palindromic_bases(_, [], []). palindromic_bases(N, [B|Bs], [B|Res1]) :- num_to_base(N, B, NB), palindrome(NB), palindromic_bases(N, Bs, Res1). palindromic_bases(N, [B|Bs], Res1) :- num_to_base(N, B, NB), \+palindrome(NB), palindromic_bases(N, Bs, Res1). /* (define (palindromes-to-n n Bases) (cond ((< n 0) '()) ((all-palindromes? n Bases) (append (palindromes-to-n (sub1 n) Bases) (list n))) (else (palindromes-to-n (sub1 n) Bases)))) */ % palindromes_to_n(+N, +Bases, -Pals) palindromes_to_n(N, _, []) :- N < 0. palindromes_to_n(N, Bases, Res) :- N >= 0, all_palindromes(N, Bases), N1 is N-1, palindromes_to_n(N1, Bases, Res1), append(Res1, [N], Res). palindromes_to_n(N, Bases, Res) :- N >= 0, \+all_palindromes(N, Bases), N1 is N-1, palindromes_to_n(N1, Bases, Res). %% ordinea clauzelor și premiselor parent(george6, elizabeth2). parent(elizabeth, elizabeth2). parent(philip, charles). parent(philip, anne). parent(philip, andrew). parent(philip, edward). parent(elizabeth2, charles). parent(elizabeth2, anne). parent(elizabeth2, andrew). parent(elizabeth2, edward). parent(charles, william). parent(charles, harry). parent(william, george). parent(william, charlotte). parent(william, louis). pred(Parent, Child) :- parent(Parent, Child). pred(Pred, Succ) :- parent(Pred, Child), pred(Child, Succ). %% clauze invers pred1(P, S) :- parent(P, C), pred1(C, S). pred1(P, C) :- parent(P, C). %% premise invers pred2(P, C) :- parent(P, C). pred2(P, S) :- pred2(C, S), parent(P, C). %% clauze și premise invers pred3(P, S) :- pred3(C, S), parent(P, C). pred3(P, C) :- parent(P, C). factorial(0, 1). factorial(N, F) :- N > 0, N1 is N-1, factorial(N1, F1), F is N * F1. factorial_1(0, 1). factorial_1(N, F) :- N > 0, factorial_1(N1, F1), N1 is N-1, F is N*F1. %% eliminarea distincției dintre intrare și ieșire elem(X, [X|_]). elem(X, [_|Rest]) :- elem(X, Rest). evens(L, Sol) :- findall(X, (elem(X, L), X mod 2 =:= 0), Sol). one_even(L) :- findall(X, (elem(X, L), X mod 2 =:= 0), [_]). %% dacă vrem și poziția pe care apare un element %% elemI(?X, ?Index, ?L) elemI(X, 0, [X|_]). elemI(X, Idx, [_|Rest]) :- elemI(X, Aux, Rest), Idx is Aux + 1. concat([], L, L). concat([X|Rest], L, [X|Rez]) :- concat(Rest, L, Rez). /* concat(X,[3,4],[1,2,3,4]). X = [1|Rest], L = [3,4], Rez = [2,3,4] si trebuie demonstrat concat(Rest, [3,4], [2,3,4]) X = [1,2|Rest'] si trebuie demonstrat concat(Rest', [3,4], [3,4]) unifica cu primul fapt cu legarea Rest' = [] => X = [1,2]. unifica si cu regula de mai jos: X = [1,2,3|Rest''] si trebuie demonstrat concat(Rest'', [3,4], [4]).. care va da fail in final */ %% exerciții concat elem_(X,L) :- concat(_, [X|_], L). last_(X,L) :- concat(_, [X], L). del22(L,Rez) :- concat([_,_|Rez], [_,_], L). %exemplu: del22([1,2,3,4,5,6],Rez) => Rez = [3,4] %% cut comp(X, Y, 'LT') :- X < Y. comp(X, Y, 'EQ') :- X =:= Y. comp(X, Y, 'GT') :- X > Y. comp_(X, Y, 'LT') :- X < Y, !. comp_(X, Y, 'EQ') :- X =:= Y, !. comp_(_, _, 'GT'). elemP(X, [X|_]) :- !. elemP(X, [_|Rest]) :- elemP(X, Rest). %% echivalent elemPP(X, [Y|Rest]) :- X = Y, ! ; elemPP(X, Rest). sibling(X, Y) :- parent(P, X), parent(P, Y), X \= Y. sibling_(X, Y) :- parent(P, X), !, parent(P, Y), X \= Y. siblingOnce(X, Y) :- once(parent(P, X)), parent(P, Y), X \= Y.