Lecture 16

wet(water).
wet(milk).
wet(orange_juice).
cost(milk,95).
mother(betty,ann).
mother(delia,betty).
father(chris,ann).
father(eric,betty).
parent(X,Y) :- mother(X,Y).
parent(X,Y) :- father(X,Y).
next(dundee,arbroath).
next(arbroath,montrose).
next(montrose, stonehaven).
next(stonehaven,aberdeen).
before(X,Y) :- next(X,Y).
before(X,Y) :- before(X,W),next(W,Y).
/* Reverse a list */
accrev([H|T],A,R) :- accrev(T,[H|A],R).
accrev([],A,A).
rev(L,R) :- accrev(L,[],R).
Back to F28PL


Lecture 17

friends(phil,robin).
friends(chris,robin).
friends(phil,pat).
friends(jo,chris).
invitation(phil).
invitation(pat).
party(P) :- invitation(P).
party(P) :- friends(P,F),invitation(F).
party(P) :- friends(F,P),invitation(F).
pals(P,F) :- friends(P,F).
pals(P,F) :- friends(F,P).
party(P) :- pals(P,F),invitation(F).
invitation(eric).
popular(P) :- party(P),!,no_computer_talk(P).
operands(X+Y,X,Y).
sumsq(X,Y,Z) :- Z is (X*X)+(Y*Y).
sum(0,0).
sum(N,S) :- N1 is N-1, sum(N1,S1),S is S1+N.
check_invitations(N) :- 
 asserta(count(0)),
 count_invitations(N).
increment :-
 retract(count(N)), N1 is N+1,
 asserta(count(N1)), !, fail.
count_invitations(N) :- invitation(P),increment.
count_invitations(N) :- retract(count(N)).
Back to F28PL


Lecture 18

squares(0,[]).
squares(N,[N1|T]) :- N1 is N*N, N2 is N-1, squares(N2,T).
llength([],0).
llength([_|T],L) :- 
 llength(T,L1), L is L1+1.
contains(_,[]) :- fail.
contains(X,[X|_]).
contains(X,[_|T]) :- contains(X,T).
find(F,[[F,S]|_],S).
find(F,[_|T],S) :- find(F,T,S).
ordered([]).	
ordered([A]).
ordered([A|[B|T]]) :- A<B,ordered([B|T]).
insert(V,[],[V]).
insert(V,[H|T],[V|[H|T]]) :- V<H.
insert(V,[H|T],[H|T1]) :- insert(V,T,T1).
ssort([],[]).
ssort([H|T],L) :- ssort(T,T1),insert(H,T1,L).
wordsToDB([]).
wordsToDB([[N,W]|T]) :- assert(word(N,W)),wordsToDB(T).
age(al,18).
age(bea,19).
age(cam,20).
age(deb,21).
people(P) :- assert(ages([])),getAges(P).
getAges(P) :- age(N,A),getAge(N,A).
getAges(P) :- retract(ages(P)).
getAge(N,A) :- retract(ages(P)),assert(ages([[N,A]|P])),!,fail.
copyTerms :- read(X),copyTerms1(X).
copyTerms1(end_of_file).
copyTerms1(X) :- write(X),nl,read(Y),copyTerms1(Y).
getTerms(L) :- read(X),getTerms1(X,L).
getTerms1(end_of_file,[]).
getTerms1(X,[X|L]) :- read(Y),getTerms1(Y,L).
getChars(L) :- get_char(X),getChars1(X,L).
getChars1(end_of_file,[]).
getChars1(X,[X|L]) :- get_char(Y),getChars1(Y,L).
copyChars :- get_char(X),copyChars1(X).
copyChars1(end_of_file).
copyChars1(X) :- put_char(X),get_char(Y),copyChars1(Y).
copyFile(X,Y) :-
 open(X,read,F1),set_input(F1),
 open(Y,write,F2),set_output(F2),
 copyChars,
 close(F1),close(F2).
Back to F28PL


Lecture 19

/* article([the|T],T).
article([a|T],T).
noun([cat|T],T).
noun([dog|T],T).
verb([saw|T],T).
verb([chased|T],T).
sentence(L,S) :-
 article(L,S1),noun(S1,S2),verb(S2,S3),article(S3,S4),noun(S4,S).
*/
/*
article --> [the].
article --> [a].
noun --> [cat].
noun --> [dog].
verb --> [saw].
verb --> [chased].
sentence --> article, noun, verb,article, noun.
*/
article(article(the)) --> [the].
article(article(a)) --> [a].
noun(noun(cat)) --> [cat].
noun(noun(dog)) --> [dog].
verb(verb(saw)) --> [saw].
verb(verb(chased)) --> [chased].
sentence(sentence(A1,N1,V,A2,N2)) --> 
 article(A1),noun(N1),
 verb(V),article(A2),noun(N2).
digit(0) --> [0].
digit(1) --> [1].
digit(2) --> [2].
digit(3) --> [3].
digit(4) --> [4].
digit(5) --> [5].
digit(6) --> [6].
digit(7) --> [7].
digit(8) --> [8].
digit(9) --> [9].
number([D|N]) --> digit(D),number(N).
number([D]) --> digit(D).
base(B) --> ['('],exp(B),[')'].
base(n(N)) --> number(N).
term(*(B,T)) --> base(B),[*],term(T).
term(/(B,T)) --> base(B),[/],term(T).
term(B) --> base(B).
exp(+(T,E)) --> term(T),[+],exp(E).
exp(-(T,E)) --> term(T),[-],exp(E).
exp(T) --> term(T).
eval(E1+E2,V) :- eval(E1,V1),eval(E2,V2),V is V1+V2.
eval(E1-E2,V) :- eval(E1,V1),eval(E2,V2),V is V1-V2.
eval(E1*E2,V) :- eval(E1,V1),eval(E2,V2),V is V1*V2.
eval(E1/E2,V) :- eval(E1,V1),eval(E2,V2),V is V1/V2.
eval(n(N),V) :- evalNumb(N,0,V).
evalNumb([],V,V).
evalNumb([H|T],V1,V) :- V2 is 10*V1+H,evalNumb(T,V2,V).
run(L,V) :- exp(T,L,_),eval(T,V).
Back to F28PL