Prolog Record
Prolog Record
Prolog Record
path(Open,_,_) :-
empty_sort_queue(Open),
write("graph searched, no solution found").
path(Open, Closed, Goal) :-
remove_sort_queue(First_record, Open, _),
state_record(State, _, _, _, _, First_record),
State = Goal,
write('Solution path is: '), nl,
printsolution(First_record, Closed).
insert_list([], L, L).
insert_list([State | Tail], L, New_L) :-
insert_sort_queue(State, L, L2),
insert_list(Tail, L2, New_L).
printsolution(Next_record, _):-
state_record(State, nil, _, _,_, Next_record),
write(State), nl.
printsolution(Next_record, Closed) :-
state_record(State, Parent, _, _,_, Next_record),
state_record(Parent, _, _, _, _, Parent_record),
member_set(Parent_record, Closed),
printsolution(Parent_record, Closed),
write(State), nl.
PROLOG PROGRAM TO DEMONSTRATE FAMILY RELATIONSHIP
/* Facts */
male(jack).
male(oliver).
male(ali).
male(james).
male(simon).
male(harry).
female(helen).
female(sophie).
female(jess).
female(lily).
parent_of(jack,jess).
parent_of(jack,lily).
parent_of(helen, jess).
parent_of(helen, lily).
parent_of(oliver,james).
parent_of(sophie, james).
parent_of(jess, simon).
parent_of(ali, simon).
parent_of(lily, harry).
parent_of(james, harry).
/* Rules */
father_of(X,Y):- male(X),
parent_of(X,Y).
mother_of(X,Y):- female(X),
parent_of(X,Y).
Output:
?mother_of(X,lily)
X=helen
PROLOG PROGRAM TO COUNT THE NUMBER OF ELEMENTS IN A LIST
OUT PUT :
? list_length([a, b, c, d], N).
N=4
DOMAINS
person = person(name,address)
name = name(first,last)
address = addr(street,city,state)
street = street(number,street_name)
city,state,street_name = string
first,last = string
number = integer
GOAL
P1 = person(name(jim,mos),addr(street(5,"1st st"),igo,"CA")),
P1 = person(name(_,mos),Address),
P2 = person(name(jane,mos),Address),
write("P1=",P1),nl,
write("P2=",P2),nl.
OUTPUT:
Jim,mos,5,1st St,igo,CA
Prolog program of Fun to show the Concept of CUT Operator
OUTPUT:
?max_find(10,20,Max).
?Max =20.
PROLOG PROGRAM TO FIND MEMBER OF A SET
list_member(b,[a,[b,c]]).
list_length([a,b,c,d,e,f,g,h,i,j],Len).
OUTPUT :
? list_member(b,[a,[b,c]]).
True
? list_member(f,[a,[b,c]]).
PROLOG PROGRAM TO CONCATENATE TWO SETS
domains
list=integer*
predicates
append(list,list,list).
clauses
append([X|L1],L2,[X|L3]) :- append(L1,L2,L3).
OUTPUT:
? append([1,2,3,4],[5,6,7,8,9],X).
X = [1, 2, 3, 4, 5, 6, 7, 8, 9]
PROLOG PROGRAM TO FIND PERMUTATION OF A SET
% domains
list = symbol*
%predicates
permute(list,list).
del(symbol,list,list).
%clauses
del(X,[X|L1],L1).
del(X,[Y|L1],[Y|L2]):-
del(X,L1,L2).
permute([],[]).
permute(L,[X|P]):-
del(X,L,L1),
permute(L1,P).
OUTPUT:
? permute([a,b,c],P).
P = [a, b, c]
P = [a, c, b]
P = [b, a, c]
P = [b, c, a]
P = [c, a, b]
P = [c, b, a]
PROLOG PROGRAM TO SOLVE NQueens PROBLEM
queens(N, Queens) :-
length(Queens, N),
board(Queens, Board, 0, N, _, _),
queens(Board, 0, Queens).
constraints(0, _, _, _) :- !.
constraints(N, Row, [R|Rs], [C|Cs]) :-
arg(N, Row, R-C),
M is N-1,
constraints(M, Row, Rs, Cs).
queens([], _, []).
queens([C|Cs], Row0, [Col|Solution]) :-
Row is Row0+1,
select(Col-Vars, [C|Cs], Board),
arg(Row, Vars, Row-Row),
queens(Board, Row, Solution).
OUTPUT:
?queens(8, Queens)
Queens =
♛
♛
♛
♛
♛
♛
♛
♛
Queens =
Queens =
♛
♛
♛
♛
♛
♛
♛
♛
Queens =