Representation of A Database: 1. Tuple Facts 2. Attribute Facts 3. List of Structures
Representation of A Database: 1. Tuple Facts 2. Attribute Facts 3. List of Structures
1. Tuple facts
2. Attribute facts
3. List of structures
NO 1
?employee1(N,100,P,S).
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.
LIST
List : simple data structure
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*
print list forward
domains
name = symbol print_list([cat,dog,bird,chicken])
anylist = name* cat
dog
predicates bird
print_list(anylist) chicken
clauses
print_list([]).
print_list([H|T])
:- write(" "),
write(H," \n"),
print_list(T).
print list reverse
domains
print_list_rev([cat,dog,bird,chicken]
name = symbol )
anylist = name* chicken
predicates bird
print_list_rev(anylist) dog
clauses cat
print_list_rev([]).
print_list_rev([H|T])
:- print_list_rev(T),
write(" "),
write(H," \n").
Write a list
domains domains
list = integer* list = symbol*
/* or whatever type you wish to use */
/* or whatever type you wish to use */
predicates predicates
1 man
write_a_list(list) write_a_list(list)
clauses 2 woman
clauses
write_a_list([]). 3 write_a_list([]). girl
/* If the list is empty, do /* If the list is empty, do
nothing more............. */ nothing more....... */
boy
write_a_list([H|T]) :- write_a_list([H|T]) :-
/* Match the head to H and /* Match the head to H and
the tail to T, then...... */ the tail to T, then....... */
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]).
Member
namelist = name* = list name = symbol
domains
namelist = name*
name = symbol NO 1
predicates ?is_a_member_of(man,
[woman,man, boy])
is_a_member_of(name,namelist)
clauses Yes
is_a_member_of(Name,[Name|_]).
/****/
is_a_member_of(Name,[_|Tail])
:- is_a_member_of(Name,Tail).
/****/
check member
domains
namelist = name* member(bird,
name = symbol [cat,dog,bird,chicke
yes n])
predicates
member(name,namelist)
clauses member(rose,
member(Name,[Name|_]). [cat,dog,bird,chicken])
no
/* find the member in the list */
member(Name,[_|Tail])
:- member(Name,Tail).
find the word in the list
domains NO 1
? findword(house,[cat,is,in,the,house])
namelist = name*
name = symbol house is in the list.
NO 2
predicates
? findword(dog,[cat,is,in,the,house])
findword(name,namelist)
dog not found.
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
? one_tuple_1(e(N,100,P,S),
3. List of structures
[e(brain, 100, operator, 20000),
e(john, 200, manager, 50000),
e(nancy,100, manager, 50000)])
N = brain
asserting a stream of tuples in a LIST Structure
/* Name Dept Position Salary */ P = operator
[e(brain, 100, operator, 20000), S = 20000
e(john, 200, manager, 50000),
N = nancy
e(nancy,100, manager, 50000)]
one_tuple_1(e(N,D,P,S),[e(N,D,P,S)|Tail]). P = manager
/*1*/ S = 50000
one_tuple_1(e(N,D,P,S),[e(_,_,_,_)|Tail])
:- one_tuple_1(e(N,D,P,S), Tail). /*2*/
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)]).
*/
Find length of list
domains NO 1.
predicates
length_of(list, integer) What = 5
clauses NO2.
length_of([_|T], L) :- No
length_of(T,TailLength),
L = TailLength + 1.
Append
domains NO 1
What = [1,2,3,4]
predicates
NO 2
append(integerlist,
?append ([1,2,3], [4,5,6,7],What)
integerlist,integerlist)
What = [1,2,3,4,5,6,7]
clauses
append([],List,List).
append([X|L1],List2,[X|L3])
:- append(L1,List2,L3).
Find average age
domains
name, address = string L = keep age list
age = integer Sum= sum all ages
list = age*
predicate
N = list length of L
person(name, address, age) Ave = average age value
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),
predicates
string_chlist(string, charlist)
clauses
string_chlist("", []).
string_chlist(S, [H|T]) :-
frontchar(S, H, S1),
write(" * ",H," * \n "),
string_chlist(S1, T).
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([], _ ).