Chapter 7 List Operator. 344-302 LP and Prolog Chapter 72 1. Tuple facts 2. Attribute facts 3. List...
-
Upload
sage-algood -
Category
Documents
-
view
233 -
download
0
Transcript of Chapter 7 List Operator. 344-302 LP and Prolog Chapter 72 1. Tuple facts 2. Attribute facts 3. List...
Chapter 7
List Operator
Chapter 7 2344-302 LP and Prolog
1. Tuple facts 2. Attribute facts3. List of structures
Representation of a database
Chapter 7 3344-302 LP and Prolog
asserting each tuple as a facts/* Name Dept Position Salary */
employee1(brain, 100, operator, 20000)employee1(john, 200, manager, 50000)employee1(nancy,100, manager, 50000)
1. Tuple facts
NO 1
?employee1(N,100,P,S).
N = brain
P = operator
S = 20000
N = nancy
P = manager
S = 50000
Chapter 7 4344-302 LP and Prolog
predicates
country(symbol)
print_countries
clauses
country(england).
country(france).
country(germany).
country(denmark).
print_countries :- country(X),
write(X), /* write the value of X */
nl, /* start a new line */
fail.
print_countries.
EX07EX01 NO 1
?print_countries.
english
france
germany
denmark
Chapter 7 5344-302 LP and Prolog
asserting each attribute as a facts attributes can be brought together as rule department(brain, 100). department(nancy, 100).position(brain,operator).position(nancy,manager).salary(brain,20000).salary(nancy,50000).
employee2(Name, Dept, Pos, Sal)
:- department(Name, Dept), position(Name, Pos), salary(Name, Sal).
2. Attribute facts
NO 1
?employee2(N,100,P,S).
N = brain
P = operator
S = 20000
N = nancy
P = manager
S = 50000
Chapter 7 6344-302 LP and Prolog
FACTEX02.prodomains thing = book(title,author) ; car(name); shoes
author = author(firstname,lastname) person,title,name,firstname,lastname,shoes
= symbol predicates ownes(person,thing)clauses ownes(siri,book("AI
book",author(tom,brook))). ownes(siri,car(honda)). ownes(siri,shoes).goal ownes(X,Y),write(X, “ owns “,Y,"\n "),fail.
Chapter 7 7344-302 LP and Prolog
LISTList : simple data structure
L = [man, woman, boy, girl]
L = [a,b,c]L = [Head|Tail]L = [a|Tail] Tail = [b,c]L = [a,b,c]= [a|[b,c]] = [a,b|[c]]
domains name = symbol anylist = name*
Chapter 7 8344-302 LP and Prolog
LISTEX02.pro : print list forwarddomains name = symbol anylist = name*
predicates print_list(anylist)
clauses print_list([]). print_list([H|T])
:- write(" "), write(H," \n"), print_list(T).
print_list([cat,dog,bird,chicken])
catdogbirdchicken
Chapter 7 9344-302 LP and Prolog
LISTEX03.pro : print list reverse
print_list_rev([cat,dog,bird,chicken])
chicken bird dog
cat
domains name = symbol anylist = name*predicates print_list_rev(anylist)clauses print_list_rev([]). print_list_rev([H|T])
:- print_list_rev(T),
write(" "), write(H," \n").
Chapter 7 10344-302 LP and Prolog
EX08EX01 Write a listdomains
list = integer*
/* or whatever type you wish to use */
predicates
write_a_list(list)
clauses
write_a_list([]).
/* If the list is empty, do
nothing more............. */
write_a_list([H|T]) :-
/* Match the head to H and
the tail to T, then...... */
write(H), nl, write_a_list(T).
goal
write_a_list([1, 2, 3]).
domains
list = symbol*
/* or whatever type you wish to use */
predicates
write_a_list(list)
clauses
write_a_list([]).
/* If the list is empty, do
nothing more....... */
write_a_list([H|T]) :-
/* Match the head to H and
the tail to T, then....... */
write(H), nl,write_a_list(T).
goal
write_a_list([man, woman,
girl, boy]).
1
2
3
man
woman
girl
boy
Chapter 7 11344-302 LP and Prolog
reverse.pro Write a list in reverse orderdomains
list = integer*
/* or whatever type you wish to use */
predicates
write_reverse_list(list)
clauses
write_reverse_list([]).
/* If the list is empty, do nothing more. */
write_reverse_list([H|T]) :-
/* Match the head to H and the tail to
T, then... */
write_reverse_list(T), nl,
write(H).
goal
write_reverse_list([1, 2, 3]).
3
2
1
Chapter 7 12344-302 LP and Prolog
EX08EX06 Member
domains
namelist = name* name = symbol
predicates
is_a_member_of(name,namelist)
clauses
is_a_member_of(Name,[Name|_]). /****/
is_a_member_of(Name,[_|Tail]) :- is_a_member_of(Name,Tail).
/****/
NO 1
?is_a_member_of(man, [woman,man, boy])
Yes
name = symbol = สมาชิ�กnamelist = name* = list
Chapter 7 13344-302 LP and Prolog
LISTEX04.pro : check member
member(bird,[cat,dog,bird,chicke
n])yes
domains namelist = name* name = symbol
predicates member(name,namelist)
clauses member(Name,[Name|_]).
/* find the member in the list */
member(Name,[_|Tail]) :-
member(Name,Tail).
member(rose,[cat,dog,bird,chicken])
no
Chapter 7 14344-302 LP and Prolog
findword.pro : find the word in the listdomains
namelist = name*
name = symbol
predicates
findword(name,namelist)
clauses
findword(Name,[]):- write(Name), write(“ not found.”),nl./*1*/
findword(Name,[Name,_]):- write(Name), write(“ is in the list.”),nl. /*2*/
findword(Name,[_|Tail]) :- findword(Name,Tail). /*3*/
NO 1
? findword(house,[cat,is,in,the,house])
NO 2
? findword(dog,[cat,is,in,the,house])
house is in the list.
dog not found.
Chapter 7 15344-302 LP and Prolog
3. List of structures
NO 1
? one_tuple_1(e(N,100,P,S), [e(brain, 100, operator, 20000),
e(john, 200, manager, 50000),
e(nancy,100, manager, 50000)])
asserting a stream of tuples in a LIST Structure/* Name Dept Position Salary */
[e(brain, 100, operator, 20000), e(john, 200, manager, 50000), e(nancy,100, manager, 50000)]
one_tuple_1(e(N,D,P,S),[e(N,D,P,S)|Tail]). /*1*/
one_tuple_1(e(N,D,P,S),[e(_,_,_,_)|Tail])
:- one_tuple_1(e(N,D,P,S), Tail). /*2*/
N = brain
P = operator
S = 20000
N = nancy
P = manager
S = 50000
Chapter 7 16344-302 LP and Prolog
LISTEX05.pro : check member predicate
domains thing = book(title,author) namelist = thing* title, author = symbolpredicates member(thing,namelist)
clauses member(Name,[Name|_]). /* find the member in the list */ member(Name,[_|Tail]) :- member(Name,Tail)./* member(book(tiger,jonh),[book(cat,jim),book(tiger,john), book(bird,jane)]).*/
Chapter 7 17344-302 LP and Prolog
EX08EX02 Find length of list
domains
list = integer*
predicates
length_of(list, integer)
clauses
length_of([], 0). /*….*/
length_of([_|T], L) :-
length_of(T,TailLength),
L = TailLength + 1.
NO 1.
? Length_of([1,2,5,6,7],What)
NO2.
?length_of([122,123,111],9)
What = 5
No
Chapter 7 18344-302 LP and Prolog
EX08EX07 Append
domains
integerlist = integer*
predicates
append(integerlist,
integerlist,integerlist)
clauses
append([],List,List).
append([X|L1],List2,[X|L3])
:- append(L1,List2,L3).
NO 1
?append ([1], [2,3,4], What)
NO 2
?append ([1,2,3], [4,5,6,7],What)
ใส�ค่�าใน L1 เอาไว้�หน�า List 2
แล้�ว้เก�บไว้�ที่�� L3
What = [1,2,3,4]
What = [1,2,3,4,5,6,7]
Chapter 7 19344-302 LP and Prolog
EX08EX08 Find average agedomains name, address = string age = integer
list = age*predicate person(name, address, age)
sumlist(list, age, integer)goal findall(Age, person(_, _, Age), L), sumlist(L, Sum, N), Ave = Sum/N, write("Average =", Ave), nl.clauses sumlist([], 0, 0). /****/
sumlist([H|T], Sum, N) :- sumlist(T, S1, N1),
Sum = H +S1, N =1+N1. /****/ person("Sherlock Holmes", "22B Baker Street", 42). person("Pete Spiers", "Apt. 22, 21st Street", 36). person("Mary Darrow", "Suite 2, Omega Home", 51).
L = keep age list
Sum= sum all ages
N = list length of L
Ave = average age value
Chapter 7 20344-302 LP and Prolog
domains list = integer*
predicates readlist(list)
goal makewindow(1, 7, 7, " Integer List ", 5, 5, 15, 70), write(" Type in a column of integers, like this:", "\n\n integer (press ENTER)\n integer (press ENTER)\n", " etc.\n\n Type X (and press ENTER) to end the list.\n\n"), readlist(TheList), write("\nThe Turbo Prolog list is: ",TheList).
clauses readlist([H|T]) :- write("\16 "), /* This prints the prompt symbol */ readint(H), !, readlist(T). readlist([]).
EX12EX08.PRO : Read Integer into the list
Chapter 7 21344-302 LP and Prolog
domains charlist = char*
predicates string_chlist(string, charlist)
clauses string_chlist("", []).
string_chlist(S, [H|T]) :- frontchar(S, H, S1), write(" * ",H," * \n "),
string_chlist(S1, T).
EX13EX01NEW.PRO :Write Character list
Chapter 7 22344-302 LP and Prolog
domains integerlist = integer*
predicates writelist(integerlist) write5(integerlist, integer)
clauses writelist(NL ) :- nl, write5(NL, 0 ), nl. write5(TL, 5 ) :- !, nl, write5(TL, 0). write5([H|T], N ) :- !, write(H," "),
N1=N+1, write5(T, N1). write5([], _ ).
EX12EX02.PRO :Writelist
Chapter 7 23344-302 LP and Prolog
LAB 5
1) จงเขี�ยนโปรแกรมภาษาโปรล้�อกเพื่#�อแสดงว้�า เล้ขีจ%านว้นเต็�ม 2 จ%านว้นหารล้งต็'ว้หร#อไม� ถ้�าหารล้งต็'ว้ให�แสดงผล้ล้'พื่ธ์+ว้�า Have solution ถ้�าหารไม�ล้งต็'ว้ให�แสดงผล้ล้'พื่ธ์+ว้�า No solution พื่ร�อมที่',งยกต็'ว้อย�างประกอบแสดงผล้การที่%างานด�ว้ย
2) จงเขี�ยนโปรแกรมภาษาโปรล้�อกเพื่#�อหาค่�าเฉล้��ยขีองเล้ขีจ%านว้นจร�ง 3 จ%านว้น พื่ร�อมที่',งยกต็'ว้อย�างประกอบแสดงผล้การที่%างานด�ว้ย
สิ่��งที่��ต้�องสิ่ง 1. Prolog Source Code 2. ผลการ run program (จากจอภาพ)วั�นก�าหนดสิ่ง วั�นที่�� 16 สิ่�งหาคม 12.00 น . หน�าห�อง
M.105
Chapter 7 24344-302 LP and Prolog