% reverse(+L, -R) % rev(T) ++ [H] myReverse(L, R) :- L = [], R = []. myReverse(L, R) :- L = [H | T], myReverse(T, TRev), append(TRev, [H], R). % +L, +Acc, -R revAcc([], Acc, R) :- Acc = R. revAcc(L, Acc, R) :- L = [H | T], revAcc(T, [H | Acc], R). % trace si nodebug pentru tracing % structuri /* ?- Z = (1,2). Z = (1, 2). ?- Z = (1,2,3). Z = (1, 2, 3). ?- Z = pos(1,2). Z = pos(1, 2). ?- pos(X, Y) = pos(1,2). X = 1, Y = 2. ?- pos(X, Y) = posA(1,2). false. ?- Struct = 1/2/3. Struct = 1/2/3. ?- X/Y/Z = 1/2/3. X = 1, Y = 2, Z = 3. ?- member((b, X, Y), [(a, 1, 2), (b, 2, 3) , (c, 4, 5)]). X = 2, Y = 3 . ?- member((Z, 2, Y), [(a, 1, 2), (b, 2, 3) , (c, 4, 5)]). Z = b, Y = 3. */ % ! = "cut" mem(E, [E|_]) :- !. mem(E, [_|T]) :- mem(E, T). min(X, Y, Min) :- X < Y, Min = X. min(X, Y, Min) :- X >= Y, Min = Y. min2(X, Y, Min) :- X < Y, Min = X. min2(_, Y, Min) :- Min = Y. min3(X, Y, Min) :- X < Y, !, Min = X. min3(_, Y, Min) :- Min = Y. min4(X, Y, Min) :- !, X < Y, Min = X. min4(_, Y, Min) :- Min = Y. este(lili, liliac). este(fifi, papagal). zboara(liliac). zboara(papagal). pasare(X) :- este(X, liliac), !, false. pasare(X) :- este(X, Y), zboara(Y). /* ?- X = [1,2,3], write(X). [1,2,3] X = [1, 2, 3]. ?- X = [1,2,3], writeln(X). [1,2,3] X = [1, 2, 3]. ?- (X,Y) = (1,2), format("X este ~w si Y este ~w", [X, Y]). X este 1 si Y este 2 X = 1, Y = 2. */ lista([1,2,3,4,5,6,7,8,9]). /* ?- lista(L), member(X, L), X < 8, X > 2. L = [1, 2, 3, 4, 5, 6, 7, 8, 9], X = 3 ; L = [1, 2, 3, 4, 5, 6, 7, 8, 9], X = 4 ; L = [1, 2, 3, 4, 5, 6, 7, 8, 9], X = 5 ; L = [1, 2, 3, 4, 5, 6, 7, 8, 9], X = 6 ; L = [1, 2, 3, 4, 5, 6, 7, 8, 9], X = 7 ; false. ?- lista(L), findall(X, (member(X, L), X < 8, X > 2), Bag). L = [1, 2, 3, 4, 5, 6, 7, 8, 9], Bag = [3, 4, 5, 6, 7]. ?- lista(L), findall((X, Y), (member(X, L), member(Y, [a,b,c])), Bag). L = [1, 2, 3, 4, 5, 6, 7, 8, 9], Bag = [(1, a), (1, b), (1, c), (2, a), (2, b), (2, c), (3, a), (3, b), (..., ...)|...]. ?- lista(L), findall((X, Y), (member(X, L), member(Y, [a,b,c])), Bag), write(Bag). [(1,a),(1,b),(1,c),(2,a),(2,b),(2,c),(3,a),(3,b),(3,c),(4,a),(4,b),(4,c),(5,a),(5,b),(5,c),(6,a),(6,b),(6,c),(7,a),(7,b),(7,c),(8,a),(8,b),(8,c),(9,a),(9,b),(9,c)] L = [1, 2, 3, 4, 5, 6, 7, 8, 9], Bag = [(1, a), (1, b), (1, c), (2, a), (2, b), (2, c), (3, a), (3, b), (..., ...)|...]. ?- lista(L), findall(Y, (member(X, L), Y is X + 1), Bag). L = [1, 2, 3, 4, 5, 6, 7, 8, 9], Bag = [2, 3, 4, 5, 6, 7, 8, 9, 10]. ?- forall(member(X, [1,2,3]), 0 =:= X mod 2). false. ?- forall(member(X, [4,2,6]), 0 =:= X mod 2). true. */