execute(Functor,ArgumentList):- Goal =.. [Functor|ArgumentList], call(Goal).
execute(Functor,ArgumentList):- Goal =.. [Functor|ArgumentList], Goal.
apply(_,[],[]). apply(Functor,[Head|Tail],[HeadResult|TailResult]):- Function =.. [Functor,Head,HeadResult], Function, apply(Functor,Tail,TailResult).
my_not(Goal):- Goal, !, fail. my_not(Goal).
borrow(Book,Date):- retract(shelf(Book)), assertz(loan(Book,Date)). return(Book,Date,Fine):- retract(loan(Book,BorrowDate)), asserta(shelf(Book)), calculate_fine(BorrowDate,Date,Fine). calculate_fine(BorrowDate,ReturnDate,25):- Days is ReturnDate - BorrowDate, Days > 14, !. calculate_fine(_,_,0). :- dynamic shelf/1. shelf(bible). shelf(koran). shelf(ekat).
make_table(RowNumbers,ColumnNumbers):- member(Row,RowNumbers), member(Column,ColumnNumbers), Product is Row*Column, assertz(product(Row,Column,Product)), fail. ----------------------------------------------------------- ?- make_table([1,2,3],[4,5,6]). no. ?- product(2,4,X). yes X = 8
:-dynamic seed/1. %----The intial seed seed(1111). %----The random number generator random(Lowest,Highest,Random):- retract(seed(Seed)), Random is Lowest + (Seed mod (Highest-Lowest+1)), New_seed is (125 * Seed + 1) mod 4096, asserta(seed(New_seed)).
%----This root encountered before get_root_number(Root,Number):- retract(current_number(Root,N1)), !, Number is N1 + 1, asserta(current_number(Root,Number)). %----First time for this root get_root_number(Root,1):- asserta(current_number(Root,1)). generate_symbol(Root,Atom):- get_root_number(Root,Number), name(Root,Name1), name(Number,Name2), append(Name1,Name2,Name), name(Atom,Name).
copy(Original,Copy):- assertz(save(Original)), retract(save(Copy)).
:-dynamic fibonacci/2. fibonacci(1,1). fibonacci(2,1). fibonacci(Number,Fibonnaci):- Number > 2, N1 is Number - 1, fibonacci(N1,F1), N2 is Number - 2, fibonacci(N2,F2), Fibonacci is F1 + F2, asserta((fibonacci(Number,Fibonacci):-!)).
clause(Head,Tail)
listing(Predicate,Arity):- functor(Head,Predicate,Arity), clause(Head,Tail), write(Head), write(':-'), write(Tail), fail.
run(true):- !. run((FirstQuery,RestOfQueries)):- !, run(FirstQuery), run(RestOfQueries). run(Query):- clause(Query,Tail), run(Tail).
findall(Variable,Query,ListOfSolutionsForTheVariable)
lecturer(olivier,ai). lecturer(john,pascal). lecturer(olivier,ai). lecturer(geoff,ai). lecturer(john,programming). lecturer(nizam,networks). lecturer(peter,graphics). lecturer(geoff,projects). lecturer(olivier,ppl). lecturer(john,graphics). -------------------------------------------------------- ?- findall(Subject,lecturer(Person,Subject),SubjectList). yes SubjectList = [ai,pascal,ai,ai,programming,networks,graphics,projects,ppl,graphics]
bagof(Variable,Query,ListOfSolutionsForTheVariable)
lecturer(olivier,ai). lecturer(john,pascal). lecturer(olivier,ai). lecturer(geoff,ai). lecturer(john,programming). lecturer(nizam,networks). lecturer(peter,graphics). lecturer(geoff,projects). lecturer(olivier,ppl). lecturer(john,graphics). -------------------------------------------------------- ?- bagof(Subject,lecturer(Person,Subject),SubjectList). yes Person = olivier SubjectList = [ai,ai,ppl] ? ; yes Person = john SubjectList = [pascal,programming,graphics] ? ; yes Person = geoff SubjectList = [ai,projects] ? etc, etc
setof(Variable,Query,ListOfSolutionsForTheVariable)
lecturer(olivier,ai). lecturer(john,pascal). lecturer(olivier,ai). lecturer(geoff,ai). lecturer(john,programming). lecturer(nizam,networks). lecturer(peter,graphics). lecturer(geoff,projects). lecturer(olivier,ppl). lecturer(john,graphics). -------------------------------------------------------- ?- setof(Subject,lecturer(Person,Subject),SubjectList). yes Person = olivier SubjectList = [ai,ppl] ? ; yes Person = john SubjectList = [graphics,pascal,programming] ? ; yes Person = geoff SubjectList = [ai,projects] ? etc, etc
findall(Variable,Goal,_):- Goal, assertz(found(Variable)), fail. findall(_,_,List):- collect_found(List). collect_found([FirstFound|RestFound]):- retract(found(FirstFound)), !, collect_found(RestFound). collect_found([]).Write a version of findall that works for nested invocations.