0% found this document useful (0 votes)
228 views

AIM:-Write A Program To Implement The Water Jug Problem. Source Code

The document describes a Prolog program that implements the water jug problem. It defines a state/4 predicate that models different states of water levels in jugs of various volumes, and uses recursion and unification to find a sequence of steps to reach a goal state from an initial empty state. It also defines list operations like length, member, append, and delete, and list translation to convert between Vietnamese number words and digits.

Uploaded by

Sandeep Sharma
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)
228 views

AIM:-Write A Program To Implement The Water Jug Problem. Source Code

The document describes a Prolog program that implements the water jug problem. It defines a state/4 predicate that models different states of water levels in jugs of various volumes, and uses recursion and unification to find a sequence of steps to reach a goal state from an initial empty state. It also defines list operations like length, member, append, and delete, and list translation to convert between Vietnamese number words and digits.

Uploaded by

Sandeep Sharma
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/ 6

EXPERIMENT-5

AIM:- Write a program to implement the water jug problem.


Source Code:state(A,B,0,0):A=:=0,
B=:=0,
write('Empty Jug [0,0]\n'),
!,write('Goal state reached\n').
state(A,B,0,0):write('Empty Jug [0,0]\n'),
(state(A,B,0,3);
state(A,B,4,0)).
state(A,B,0,3):A=:=0,
B=:=3,
write('Fill 3 lt jug [0,3]\n'),
!,write('Goal State Reached\n').
state(A,B,0,3):write('Fill 3 lt jug [0,3]\n'),
(state(A,B,3,0);
state(A,B,4,3)).
state(A,B,3,0):A=:=3,
B=:=0,
write('Empty 3 lt jug into 4 lt jug [3,0]\n'),
!,write('Goal state reached\n').
state(A,B,3,0):write('Empty 3 lt jug into 4 lt jug [3,0]\n'),
(state(A,B,3,3);
state(A,B,4,0)).
state(A,B,4,3):A=:=4,
B=:=3,
write('Fill 4 lt jug [4,3]\n'),
!,write('Goal State Reached\n').
state(A,B,4,3):write('Fill 4 lt jug [4,3]\n'),
state(A,B,4,0).
state(A,B,3,3):A=:=3,
B=:=3,
write('Fill 3 lt jug [3,3]\n'),
!,write('Goal state reached\n').

state(A,B,3,3):write('Fill 3 lt jug [3,3]\n'),


state(A,B,4,2).
state(A,B,4,2):A=:=4,
B=:=2,
write('Fill 4 lt jug from 3 lt jug [4,2]\n'),
!,write('Goal state reached\n').
state(A,B,4,2):write('Fill 4 lt jug from 3 lt jug [4,2]\n'),
state(A,B,0,2).
state(A,B,0,2):A=:=0,
B=:=2,
write('Empty 4 lt jug [0,2]\n'),
!,write('Goal state reached\n').
state(A,B,0,2):write('Empty 4 lt jug [0,2]\n'),
state(A,B,2,0).
state(A,B,2,0):A=:=2,
B=:=0,
write('Empty 3 lt jug into 4 lt jug [2,0]\n'),
!,write('Goal state reached\n').
state(A,B,2,0):write('Empty 3 lt jug into 4 lt jug [2,0]\n'),
state(A,B,2,3).
state(A,B,2,3):A=:=2,
B=:=3,
write('Fill 3 lt jug [2,3]\n'),
!,write('Goal state reached\n').
state(A,B,4,0):A=:=4,
B=:=0,
write('Fill 4 lt jug [4,0]\n'),
!,write('Goal State Reached\n').
state(A,B,4,0):write('Fill 4 lt jug [4,0]\n'),
(state(A,B,1,3);
state(A,B,4,3)).
state(A,B,1,3):A=:=1,
B=:=3,
write('Fill 3 lt jug from 4 lt jug [1,3]\n'),
!,write('Goal State Reached\n').

state(A,B,1,3):write('Fill 3 lt jug from 4 lt jug [1,3]\n'),


(state(A,B,1,0);
state(A,B,0,3)).
state(A,B,1,0):A=:=1,
B=:=0,
write('Empty 3 lt jug [1,0]\n'),
!,write('Goal State Reached\n').
state(A,B,1,0):write('Empty 3 lt jug [1,0]\n'),
state(A,B,0,1).
state(A,B,0,1):A=:=0,
B=:=1,
write('Empty 4 lt jug into 3 lt jug [0,1]\n'),
!,write('Goal State Reached\n').
state(A,B,0,1):write('Empty 4 lt jug into 3 lt jug [0,1]\n'),
state(A,B,4,1).
state(A,B,4,1):A=:=4,
B=:=1,
write('Fill 4 lt jug [4,1]\n'),
!,write('Goal State Reached\n').
state(A,B,4,1):write('Fill 4 lt jug [4,1]\n'),
state(A,B,2,3).
state(A,B,2,3):A=:=2,
B=:=3,
write('Fill 3 lt jug from 4 lt jug [2,3]\n'),
!,write('Goal State Reached\n').
state(A,B,2,3):write('Fill 3 lt jug from 4 lt jug [2,3]\n'),
state(A,B,3,2).
state(A,B,3,2):A=:=3,
B=:=2,
write('Swap 4 and 3 lt jugs [3,2]\n'),
!,write('Goal State Reached\n').

/**
Output:
?- consult('waterjugnoo.pl').

true.

?- state(2,0,0,0).
Empty Jug [0,0]
Fill 3 lt jug [0,3]
Empty 3 lt jug into 4 lt jug [3,0]
Fill 3 lt jug [3,3]
Fill 4 lt jug from 3 lt jug [4,2]
Empty 4 lt jug [0,2]
Empty 3 lt jug into 4 lt jug [2,0]
Goal state reached
true .
*/

EXPERIMENT-4
AIM: - Define list operation and list translation.
Source Code:
length([],0).
length([Head|Tail],L):length(Tail,N),
L is 1+N.
last([X],X).
last([_|T],L):last(T,L).
member(H,[H|_]).
member(X,[_|T]):member(X,T).
append([],L,L).
append([H1|T1],L1,[H1,T3]):append(T1,L1,T3).
delete(X,[X|Tail],Tail).
delete(X,[Y|Tail],[Y|Z]):delete(X,Tail,Z).
tran(nung,one).
tran(song,two).
tran(sam,three).
tran(si,four).
tran(ha,five).
tran(hok,six).
tran(chet,seven).
tran(pat,eight).
tran(cow,nine).
tran(sip,ten).
listtran(X,Y):length(X,L),
L=:=1,
X=[Head|Tail],
tran(Head,Z),
append([],[Z],Y).
listtran(X,Y):X=[Head|Tail],
tran(Head,Z),
listtran(Tail,Ans),
append([Z],Ans,Y).

You might also like