Presentation is loading. Please wait.

Presentation is loading. Please wait.

Artificial Intelligence Programming ,Spring 2016

Similar presentations


Presentation on theme: "Artificial Intelligence Programming ,Spring 2016"— Presentation transcript:

1 Artificial Intelligence Programming ,Spring 2016
Problems on Prolog Artificial Intelligence Programming ,Spring 2016

2 Question 1 Assume the following database in Prolog.
connected(houston,dallas). connected(houston,galveston). connected(houston,san-antonio). connected(dallas,oklahoma-city). connected(dallas,austin). connected(dallas,littlerock). connected(galveston,corpus-christi). connected(galveston,clearlake). connected(san-antonio,austin). 3-way-connection(X,Y) :- connected(X,Z), !, connected(Z,Y).  What would the answer be to the following query?  ?-3-way-connection(houston,X).

3 Question 1 Assume the following database in Prolog.
connected(houston,dallas) connected(houston,galveston) connected(houston,san-antonio) connected(dallas,oklahoma-city) connected(dallas,austin) connected(dallas,littlerock) connected(galveston,corpus-christi) connected(galveston,clearlake) connected(san-antonio,austin) 3-way-connection(X,Y) :- connected(X,Z), !, connected(Z,Y) What would the answer be to the following query? ? 3-way-connection(houston,X). ? oklahoma-city; ? austin; ? littlerock ? 3-way-connection(houston,X). 3-way-connection(houston,X) :- connected(houston, Z), !, connected(Z,X). connected(houston, Z). => Z=dallas. connected(dallas, oklahoma-city). => X= oklahoma-city connected(dallas, austin). => X= austin connected(dallas, austin). => X= littlerock Stop! No more values of Z are to be checked.

4 Question 2 Consider the following program to find the second last element of a list secondLast([X,_], X). secondLast([_|T], X) :- secondLast(T, X). Which of the following is the output of the query ? secondLast([2,[3,4],5],Z). Z=false Z=4 Z=[3,4] It will throw an error

5 Question 2 Consider the following program to find the second last element of a list secondLast([X,_], X). secondLast([_|T], X) :- secondLast(T, X). Which of the following is the output of the query ? secondLast([2,[3,4],5],Z). Z=false Z=4 Z=[3,4] It will throw an error Answer: c

6 Question 2 Solution Explained
Program: secondLast([X,_], X). secondLast([_|T], X) :- secondLast(T, X). ?-secondLast([2,[3,4],5],Z). Matching with the first statement: secondLast([2,[3,4],5],Z). – False , since [2,[3,4],5] is a list with 3 elements. Matching with second statement: secondLast([_|[3,4],5],Z):- secondLast([3,4],5],Z). ?- secondLast([3,4],5],Z). secondLast([3,4],5],Z).– True. Thus Z= [3,4].

7 Question 3 What is the purpose of the following program?
function([],0). function ([_|T],R):- function (T,R1),R is R1+1. It finds the last element of a list. It finds the sum of the elements in a list It finds the number of elements in a list. None of the above.

8 Question 3 What is the purpose of the following program? Answer: c
function([],0). function ([_|T],R):- function (T,R1),R is R1+1. It finds the last element of a list. It finds the sum of the elements in a list It finds the number of elements in a list. None of the above. Answer: c

9 Question 3 Solution Explained
What is the purpose of the following program? function([],0). function ([_|T],R):- function (T,R1),R is R1+1. Answer: c : It finds the number of elements in a list. ?- function([1,2,3],X). function([],0) FALSE as it is not equal to function([1,2,3],X). function ([_|2,3],X):- function ([2,3],X1),X is X1+1 function([],0) FALSE as it is not equal to function([2,3],X1). function ([_|3],X1):- function ([3],X1’),X1 is X1’+1 function([],0) FALSE as it is not equal to function([3],X1’). function ([_|[]],X1’):- function ([],X1’’),X1’ is X1’’+1 function([],0) True . Thus X1’’ =0 Thus X1’ = X1’’ +1 =1 Thus X1= X1’+1 =2 Thus X = X1 +1 =3

10 Question 4 Which one of these programs finds the factorial of a number? a) factorial(0,1). factorial (N,F) :- N>0, N1 is N+1, factorial(N1,F1), F is N * F1. b) factorial (0,1). factorial (N,F) :- N>0, N1 is N-1, factorial(N,F1), F is N * F1. c) factorial (0,1). factorial (N,F) :- N>0, N1 is N-1, factorial(N1,F1), F is N * F1. d) factorial(0,1). factorial(N,F) :- N>0, N1 is N-1, factorial(N1,F1), F is N1 * F1.

11 Question 4 Which one of these programs finds the factorial of a number? a) factorial(0,1). factorial (N,F) :- N>0, N1 is N+1, factorial(N1,F1), F is N * F1. b) factorial (0,1). factorial (N,F) :- N>0, N1 is N-1, factorial(N,F1), F is N * F1. c) factorial (0,1). factorial (N,F) :- N>0, N1 is N-1, factorial(N1,F1), F is N * F1. d) factorial(0,1). factorial(N,F) :- N>0, N1 is N-1, factorial(N1,F1), F is N1 * F1. Answer: c

12 Question 5 Write a PROLOG program that does the following. The program should compare two lists L1 and L2 and determine if the first element of list L1 is the same as the last element of L2. Call the main predicate: first_last(List1, List2). Example:  ?- first_last([],[4,5,6,2]) no  ?- first_last([1,2],[2,3,4,5,1]) yes  ?- first_last([1,2],[1,2,3]) no

13 Question 5 Write a PROLOG program that does the following. The program should compare two lists L1 and L2 and determine if the first element of list L1 is the same as the last element of L2. Call the main predicate: first_last(List1, List2). Example:  ?- first_last([],[4,5,6,2]). no  ?- first_last([1,2],[2,3,4,5,1]). yes  ?- first_last([1,2],[1,2,3]). no Answer: first_last([H|T],[H]). first_last(L,[A|B]) :- first_last(L,B).

14 Question 6 Write a prolog program (i.e., set of predicates) that implements the following function: The program should compare two lists L1 and L2 and determine if the first list L1 is a subset of the second list L2. Call the main predicate: subset(List1, List2). Note 1: The empty list is a subset of any other list. Note 2: You can use the predicate member(X,Y) that is true if element X is a member of list Y. Example: ?- subset([],[4,5,6,2]). yes ?- subset([1,2],[a,1,e,2]). ?- subset([1,2],[a,b,e,2]). no

15 Question 6 Write a prolog program (i.e., set of predicates) that implements the following function: The program should compare two lists L1 and L2 and determine if the first list L1 is a subset of the second list L2. Call the main predicate: subset(List1, List2). Note 1: The empty list is a subset of any other list. Note 2: You can use the predicate member(X,Y) that is true if element X is a member of list Y. Example: ?- subset([],[4,5,6,2]) yes ?- subset([1,2],[a,1,e,2]) ?- subset([1,2],[a,b,e,2]) no Answer: subset([],_). subset([H|T],S) :- member(H,S), subset(T,S).

16 Question 7 Which of the following adds a statement to the knowledge base? consult retract assertz write

17 Question 7 Which of the following adds a statement to the knowledge base? consult retract assertz write Answer : c

18 Question 8 Write a program to find the nth element of a list . nth_element(X,[X|_],1). nth_element(X,[_|L],N) :- N> 1, N1 is N- 1, nth_element(X,L,N1). Query Example: ?- nth_element(X,[1,2,3,6,3,2,5,6],5). X = 3 ;

19 Question 9 The following prolog program implements a stack:
stack(Top,Stack,[Top|Stack]). Assume S1=[1,2,3] is a stack . If 1 has to be replaced by 4 which of the following command can do it? stack(X,Y,[1,2,3]), stack(4,X,Z). stack(Z,Y,[1,2,3]), stack(4,Y,Z). stack(X,Z,[1,2,3]), stack(4,Y,Z). stack(X,Y,[1,2,3]), stack(4,Y,Z).

20 Question 9 The following prolog program implements a stack:
stack(Top,Stack,[Top|Stack]). Assume S1=[1,2,3] is a stack . If 1 has to be replaced by 4 , which of the following command can do it? stack(X,Y,[1,2,3]), stack(4,X,Z). stack(Z,Y,[1,2,3]), stack(4,Y,Z). stack(X,Z,[1,2,3]), stack(4,Y,Z). stack(X,Y,[1,2,3]), stack(4,Y,Z). Answer : d


Download ppt "Artificial Intelligence Programming ,Spring 2016"

Similar presentations


Ads by Google