prolog cheat sheet

Post on 13-Dec-2015

4 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

% member( E, L ) should be true if% E is a top-level member of the list L% member should work when E is a variable or bound to a value;% L should only be bound to a value%member( E, [E|_] ).member( E, [_|R] ) :- member( E, R ).

% range( L, H, List ) should be true if% List is a list of integers from L to H-1, % inclusive. Both L and H need to be bound to ints as inputs.% The third input, List, will then be generated.%range(L,H,[]) :- L >= H.range(L,H,[L|R]) :- L < H, M is L+1, range(M,H,R).

%% [Part 1] rev( L, Rev )

rev([],[]). rev([H|T],R):- rev(T,RevT), append(RevT,[H],R).

%% [Part 2] count( E, L, N )

count(E, [], N).count(E, [F|R], N) :- F \= E, count(E, R, N).count(E, [E|R], N) :- A = N + 1, count(E, R, A).

%% [Part 3] find( Pattern, Target, Index )

find([P], [P|Y], 0).find([A|B], [A|Y], 0) :- B \== [], find(B, Y, 0).find([A|B], [X|Y], I) :- B \== [], find([A|B], Y, H), I is H+1.

%% [Part 4] depth( E, Tree, N )

depth(E,[E, _, _],0).depth(E,[A, X, _],N):- depth(E, X, Y), N is Y + 1, E < A.depth(E,[A, _, X],N):- depth(E, X, Y), N is Y + 1, A < E.

%% [Part 5] insert( E, Tree, NewTree )

insert(E, [], [E, [],[]]).insert(E, [E, _, _], [E, _, _]).insert(E, [Node, Left, Right], [Node, NewLeft, Right]) :- insert(E, Left, NewLeft), E<Node. insert(E, [Node, Left, Right], [Node, Left, NewRight]) :- insert(E, Right, NewRight), E>Node.

%% [Part 6] path( A, B, Graph, Path )

path(X, X, _, [X]).path(X, Y, [[Src,Dst]|Rest], Path) :- path(X, Src, Rest, Newpath1), path(Dst, Y, Rest, Newpath2), append(Newpath1, Newpath2, Path) ; path(X, Y, Rest, Path).

noVars( t ).noVars( f ).noVars( [_, L, R] ) :- noVars( L ), noVars( R ).noVars( [_, S] ) :- noVars( S ).

getVar( N, N ) :- number( N ).getVar( [_, Sub], N ) :- getVar( Sub, N ).getVar( [_, L, _], N ) :- getVar( L, N ).getVar( [_, L, R], N ) :- noVars(L), getVar( R, N ).

getVar( N, N ) :- number( N ).getVar( [_, Sub], N ) :- getVar( Sub, N ).getVar( [_, L, _], N ) :- getVar( L, N ).getVar( [_, L, R], N ) :- noVars(L), getVar( R, N ).

% eval

eval(t,t).eval(f,f).eval( [not, L], t ) :- eval(L, f).eval( [not, L], f ) :- eval(L, t).eval( [or, L, R], t) :- eval(L,t) ; eval(R,t).eval( [or, L, R], f) :- eval(L,f), eval(R,f).eval( [and, L, R], t) :- eval(L,t), eval(R,t).eval( [and, L, R], f) :- eval(L,f); eval(R,f).eval( [ifthen, L, R], t) :- eval(L,f) ; eval(R,t).eval( [iff, L, R], t) :- L == R.

logic(Seats) :-Seats = [[bruno, _, _], _, [_, hmc, spam], _, [dino, _, _]],nextTo([algird, _, _], [_, hmc, _], Seats),nextTo([collette, _, _], [_, _, chocolate], Seats),nextTo([collette, _, _], [_, _, donuts], Seats),lr([_, _, chocolate], [_, _, pez], Seats),nextTo([_, pomona, _], [_, _, jots], Seats),nextTo([_, pomona, _], [_, _, spam], Seats),seats(Seats),\+nextTo([dino, _, _], [_, _, donuts], Seats),\+nextTo([edwina, _, _], [_, cmc, _], Seats),\+member([bruno, scripps, _], Seats),\+member([dino, scripps, _], Seats),\+member([algird, scripps, _], Seats).

%% X is a child of Y if Y is a parent of X%child(X, Y) :- parent(Y, X).

%% if X is female and X is a parent of Y%mother(X, Y) :- female(X), parent(X, Y).

anc(X, Y) :- parent(X, Y).anc(X, Y) :- parent(Z, Y), anc(X, Z).

% run_tests(grandparent):- begin_tests(grandparent).test(grandparentT1, [nondet]) :- grandparent(jackie, bart).test(grandparentT2, [nondet]) :- grandparent(john, bart).test(grandparentT3, [nondet]) :- grandparent(helga, homer).test(grandparentT4) :-

setof(OneGP, grandparent(OneGP, bart), AllGPs),

AllGPs == [homericus,jackie,john,matilda].test(grandparentT5) :- \+grandparent(marge, homer).% add additional tests below:test(grandparentT5) :- \+ grandparent(homer, homer).

:- end_tests(grandparent).

:- dynamic thing_at/2, player_at/1, studied/1, office_hours/1, ae/1, inv/1.

player_at(jacobs).

retract(thing_at(X, Place)),assert(thing_at(X, in_hand)),

invAddOne :- inv(N), NewN is N + 1, retract(inv(N)), assert(inv(NewN)).

top related