Skip to content

Instantly share code, notes, and snippets.

@flaviocdc
Created December 2, 2012 21:55
Show Gist options
  • Save flaviocdc/4191243 to your computer and use it in GitHub Desktop.
Save flaviocdc/4191243 to your computer and use it in GitHub Desktop.
IA - Prolog
% slides
member(X, [X|_]) :- !.
member(X,[_|Y]) :- member(X,Y).
append([], L, L).
append([X|L1], L2, [X|L3]) :- append(L1, L2, L3).
add(X, L, [X|L]).
del(X, [X|Tail], Tail).
del(X, [Y|Tail], [Y|Tail1]) :- del(X, Tail, Tail1).
insert(X, List, BiggerList) :- del(X, BiggerList, List).
perm([], []).
perm([X|L], P) :- perm(L, L1), insert(X, L1, P).
% lista - ex3
size([], 0).
size([_|Tail], S) :- size(Tail, S1), S is S1 + 1.
even(L) :- size(L, S), X is S mod 2, X = 0.
odd(L) :- not(even(L)).
% lista - ex4 - TODO
lshift([H|T],L):-append(T,[H],[X|Y]),append(Y,[X],L).
% lista - ex5
inverso(X,Y):-rev(X,[],Y).
rev([],L,L).
rev([H|T],L2,L3):-rev(T,[H|L2],L3)
% lista - ex6
palindromo(L) :- inverso(L, L).
% lista - ex7
filter([], _, _, []).
filter([H|T], A, B, S) :- H<A, filter(T, A, B, S).
filter([H|T], A, B, S) :- H>B, filter(T, A, B, S).
filter([H|T], A, B, [H|S]) :- H>=A, H=<B, filter(T, A, B, S).
% lista - ex10
union([X|Y],Z,W) :- member(X,Z), union(Y,Z,W).
union([X|Y],Z,[X|W]) :- not(member(X,Z)), union(Y,Z,W).
union([],Z,Z).
% lista - ex11
set([], []).
set([X|L], [X|Lr]) :- not(member(X,L)), set(L, Lr).
set([X|L], Lr) :- member(X,L), set(L, Lr).
% lista - ex13
p(1).
p(2) :-!.
p(3).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment