Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 List Operator. 344-302 LP and Prolog Chapter 72 1. Tuple facts 2. Attribute facts 3. List of structures Representation of a database.

Similar presentations


Presentation on theme: "Chapter 7 List Operator. 344-302 LP and Prolog Chapter 72 1. Tuple facts 2. Attribute facts 3. List of structures Representation of a database."— Presentation transcript:

1 Chapter 7 List Operator

2 LP and Prolog Chapter Tuple facts 2. Attribute facts 3. List of structures Representation of a database

3 LP and Prolog Chapter 73 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 = N = nancy P = manager S = 50000

4 LP and Prolog Chapter 74 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

5 LP and Prolog Chapter 75 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 = N = nancy P = manager S = 50000

6 LP and Prolog Chapter 76 FACTEX02.pro domains 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.

7 LP and Prolog Chapter 77 LIST List : 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*

8 LP and Prolog Chapter 78 LISTEX02.pro : print list forward domains 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]) cat dog bird chicken

9 LP and Prolog Chapter 79 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").

10 LP and Prolog Chapter 710 EX08EX01 Write a list domains 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]) man woman girl boy

11 LP and Prolog Chapter 711 reverse.pro Write a list in reverse order domains 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])

12 LP and Prolog Chapter 712 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

13 LP and Prolog Chapter 713 LISTEX04.pro : check member member(bird,[cat,do g,bird,chicken]) 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,b ird,chicken]) no

14 LP and Prolog Chapter 714 findword.pro : find the word in the list domains 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.

15 LP and Prolog Chapter 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 = N = nancy P = manager S = 50000

16 LP and Prolog Chapter 716 LISTEX05.pro : check member predicate domains thing = book(title,author) namelist = thing* title, author = symbol predicates 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)]). */

17 LP and Prolog Chapter 717 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

18 LP and Prolog Chapter 718 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]

19 LP and Prolog Chapter 719 EX08EX08 Find average age domains 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

20 LP and Prolog Chapter 720 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

21 LP and Prolog Chapter 721 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

22 LP and Prolog Chapter 722 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

23 LP and Prolog Chapter 723 LAB 5 1) จงเขียนโปรแกรมภาษาโปรล็อกเพื่อแสดงว่า เลขจำนวนเต็ม 2 จำนวนหารลงตัวหรือไม่ ถ้าหาร ลงตัวให้แสดงผลลัพธ์ว่า Have solution ถ้า หารไม่ลงตัวให้แสดงผลลัพธ์ว่า No solution พร้อมทั้งยกตัวอย่างประกอบแสดงผลการทำงาน ด้วย 2) จงเขียนโปรแกรมภาษาโปรล็อกเพื่อหา ค่าเฉลี่ยของเลขจำนวนจริง 3 จำนวน พร้อมทั้ง ยกตัวอย่างประกอบแสดงผลการทำงานด้วย สิ่งที่ต้องส่ง 1. Prolog Source Code 2. ผลการ run program ( จากจอภาพ ) วันกำหนดส่ง วันที่ 16 สิงหาคม น. หน้า ห้อง M.105

24 LP and Prolog Chapter 724


Download ppt "Chapter 7 List Operator. 344-302 LP and Prolog Chapter 72 1. Tuple facts 2. Attribute facts 3. List of structures Representation of a database."

Similar presentations


Ads by Google