0% found this document useful (0 votes)
29 views3 pages

Sheet1 Logic

The document contains a student's answers to 4 questions: 1) It defines a predicate to get the position of a character in the alphabet. 2) It defines predicates for locations and uses backward chaining to determine if a location is in a given region. 3) It writes a program to check if a number is odd or even. 4) It writes a program to copy a file while counting the characters.

Uploaded by

Andrew Magdy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

Sheet1 Logic

The document contains a student's answers to 4 questions: 1) It defines a predicate to get the position of a character in the alphabet. 2) It defines predicates for locations and uses backward chaining to determine if a location is in a given region. 3) It writes a program to check if a number is odd or even. 4) It writes a program to copy a file while counting the characters.

Uploaded by

Andrew Magdy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Sheet1

Name: Mai Bomadian Amin


Class: 6
Question 1
each goal of the following goals
1- Goal
alphabet_pos(What, 15).
What= ‘o’
1 result
2- alphabet_pos(What, Where). What= ‘a’ Where=1 What=’b’ Where =2 . . . What=’z’
Where=26?
Answer:
predicates
nondeterm alphabet_pos(char, integer)
clauses
alphabet_pos('a', 1).
alphabet_pos('b', 2).
alphabet_pos('c', 3).
alphabet_pos('d', 4).
alphabet_pos('e', 5).
alphabet_pos('f', 6).
alphabet_pos('g', 7).
alphabet_pos('h', 8).
alphabet_pos('i', 9).
alphabet_pos('j', 10).
alphabet_pos('k', 11).
alphabet_pos('l', 12).
alphabet_pos('m', 13).
alphabet_pos('n', 14).
alphabet_pos('o', 15).
alphabet_pos('p', 16).
alphabet_pos('q', 17).
alphabet_pos('r', 18).
alphabet_pos('s', 19).
alphabet_pos('t', 20).
alphabet_pos('u', 21).
alphabet_pos('v', 22).
alphabet_pos('w', 23).
alphabet_pos('x', 24).
alphabet_pos('y', 25).
alphabet_pos('z', 26).
Goal
alphabet_pos(WHAT, 15).
Question 2: By having the following Clauses
1-located_in (atlanta , georgia ) .
2-located_in (houston , texas ) .
3-located_in (austin , texas ) .
4-located_in (T=toronto , ontaria) .
5-located_in (X , usa) :- located_in (X , georgia).
6-located_in (X , usa) :- located_in (X , texas) .
7-located_in (X, canda) :- located_in(X,ontaria).
8-located_in(X, north_america) :- located_in(X,usa) .
9-located_in(X , north_america) :- located_in (X , canda).
Answer the following Goals
1- located_in (houston , usa ).
2- located_in(toronto , north_america)
By writing how you get the answer of the goal showing how the program will get the answer
(Bachtracking)
Answer
1- located_in (houston , usa ).
located_in (X, usa ):- located_in (X, texas ).
located_in (houston , usa ):- located_in (houston , texas ).
Yes

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).

You might also like