Professional Documents
Culture Documents
2- located_in(toronto , north_america).
located_in(X , north_america):- located_in (X , canda).
located_in (X , canda):- located_in(X,ontaria).
located_in(X,ontaria).
located_in(toranto,ontaria)
located_in (toranto, canda):- located_in(toranto,ontaria)
located_in(toranto , north_america):- located_in (toranto, canda).
Yes.
Question3 : Write a program to check if a number is odd or even
predicates
nondeterm check(integer)
clauses
check(X):-X mod 2 =:= 0.
check(X):-X mod 2 =/= 0,write("odd").
goal
check(4).
Question 4 Write a prolog program to copy text from file named “infile.txt” to another
file named “outfile.txt” and calculate the number of characters
Answer:
domains
file=inf;outf
predicates
nondeterm readterm(integer).
nondeterm count(integer,integer).
nondeterm check(char,integer,integer).
clauses
readterm(Total):-
disk("D:\\"),openread(inf,"infile.txt"),readdevice(inf),openwrite(outf,"outfile.txt"),writede
vice(outf),count(0,Total).
count(OLD,RESULT):-eof(inf),closefile(inf),closefile(outf),count(OLD,RESULT).
count(OLD,RESULT):-not(eof(inf)),readchar(T),check(T,OLD,RESULT).
check(T,OLD,RESULT):-T=32,write(T),count(OLD,RESULT).
check(T,OLD,RESULT):-T=46,write(T),count(OLD,RESULT).
check(T,OLD,RESULT):-write(T),NEW=OLD+1,count(RESULT,NEW).
goal
readterm(X).