Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 List Operator.

Similar presentations


Presentation on theme: "Chapter 7 List Operator."— Presentation transcript:

1 Chapter 7 List Operator

2 Representation of a database
1. Tuple facts 2. Attribute facts 3. List of structures Chapter 7

3 1. Tuple facts employee1(brain, 100, operator, 20000)
?employee1(N,100,P,S). N = brain P = operator S = 20000 N = nancy P = manager S = 50000 1. Tuple facts 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) Chapter 7

4 EX07EX01 english france germany denmark NO 1 ?print_countries.
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. english france germany denmark Chapter 7

5 2. Attribute facts employee2(Name, Dept, Pos, Sal)
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). NO 1 ?employee2(N,100,P,S). N = brain P = operator S = 20000 N = nancy P = manager S = 50000 Chapter 7

6 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. Chapter 7

7 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* Chapter 7

8 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 Chapter 7

9 LISTEX03.pro : print list reverse
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"). print_list_rev([cat,dog,bird,chicken]) chicken bird dog cat Chapter 7

10 EX08EX01 Write a list 1 man 2 woman 3 girl boy goal goal 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]). 1 2 3 man woman girl boy Chapter 7

11 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]). 3 2 1 Chapter 7

12 EX08EX06 Member Yes namelist = name* = list name = symbol = สมาชิก
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 Chapter 7

13 LISTEX04.pro : check member
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(bird,[cat,dog,bird,chicken]) yes member(rose,[cat,dog,bird,chicken]) no Chapter 7

14 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. Chapter 7

15 3. List of structures one_tuple_1(e(N,D,P,S),[e(N,D,P,S)|Tail]). /*1*/
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)]) 3. List of structures N = brain P = operator S = 20000 N = nancy P = manager S = 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*/ Chapter 7

16 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)]). */ Chapter 7

17 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

18 EX08EX07 Append What = [1,2,3,4] ใส่ค่าใน L1 เอาไว้หน้า List 2
NO 1 ?append ([1], [2,3,4], What) NO 2 ?append ([1,2,3], [4,5,6,7],What) ใส่ค่าใน L1 เอาไว้หน้า List 2 แล้วเก็บไว้ที่ L3 domains integerlist = integer* predicates append(integerlist, integerlist,integerlist) clauses append([],List,List). append([X|L1],List2,[X|L3]) :- append(L1,List2,L3). What = [1,2,3,4] What = [1,2,3,4,5,6,7] Chapter 7

19 EX08EX08 Find average age L = keep age list Sum= sum all ages
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 Chapter 7

20 EX12EX08.PRO : Read Integer into the list
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([]). Chapter 7

21 EX13EX01NEW.PRO :Write Character list
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). Chapter 7

22 EX12EX02.PRO :Writelist 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([], _ ). Chapter 7

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

24 Determine that a thing can
and shall be done. and then….. we shall find the way. Chapter 7


Download ppt "Chapter 7 List Operator."

Similar presentations


Ads by Google