KEMBAR78
Prolog Cheat Sheet | PDF
0% found this document useful (0 votes)
2K views2 pages

Prolog Cheat Sheet

This document contains definitions and logic rules for several predicates including: 1. member/2 defines membership of an element in a list 2. range/3 generates a list of integers between two values 3. eval/2 evaluates boolean expressions 4. Several additional predicates are defined including rev/2, count/2, find/3, depth/4, insert/5, path/4, getVar/2, noVars/1 The logic rule logic/1 defines constraints on seating arrangements with predicates like nextTo/3. Additional definitions include child/2, mother/2, anc/2 and a test case for grandparent/2. Dynamic predicates are also declared.

Uploaded by

la
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views2 pages

Prolog Cheat Sheet

This document contains definitions and logic rules for several predicates including: 1. member/2 defines membership of an element in a list 2. range/3 generates a list of integers between two values 3. eval/2 evaluates boolean expressions 4. Several additional predicates are defined including rev/2, count/2, find/3, depth/4, insert/5, path/4, getVar/2, noVars/1 The logic rule logic/1 defines constraints on seating arrangements with predicates like nextTo/3. Additional definitions include child/2, mother/2, anc/2 and a test case for grandparent/2. Dynamic predicates are also declared.

Uploaded by

la
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

%member(E,L)shouldbetrueif

%EisatoplevelmemberofthelistL
%membershouldworkwhenEisavariableorbound
toavalue;
%Lshouldonlybeboundtoavalue
%
member(E,[E|_]).
member(E,[_|R]):member(E,R).

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

%range(L,H,List)shouldbetrueif
%ListisalistofintegersfromLtoH1,
%inclusive.BothLandHneedtobeboundtoints
asinputs.
%Thethirdinput,List,willthenbegenerated.
%
range(L,H,[]):L>=H.
range(L,H,[L|R]):L<H,MisL+1,range(M,H,R).

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.

%%[Part1]rev(L,Rev)
rev([],[]).
rev([H|T],R):rev(T,RevT),append(RevT,[H],R).
%%[Part2]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).
%%[Part3]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),IisH+1.
%%[Part4]depth(E,Tree,N)
depth(E,[E,_,_],0).
depth(E,[A,X,_],N):depth(E,X,Y),NisY+1,E
<A.
depth(E,[A,_,X],N):depth(E,X,Y),NisY+1,A
<E.
%%[Part5]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.
%%[Part6]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).

%eval

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).
%%XisachildofYifYisaparentofX
%
child(X,Y):parent(Y,X).
%%ifXisfemaleandXisaparentofY
%
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).
%addadditionaltestsbelow:
test(grandparentT5):\+grandparent(homer,homer).
:end_tests(grandparent).
:dynamicthing_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),
NewNisN+1,

retract(inv(N)),
assert(inv(NewN)).

You might also like