Showing posts with label Artificial Intelligence Programs Using Prolog. Show all posts
Showing posts with label Artificial Intelligence Programs Using Prolog. Show all posts

Write a PROLOG program that will take grammar rules in the following format: NT (NT | T)* Where NT is any nonterminal, T is any terminal and Kleene star (*) signifies any number of repetitions, and generate the corresponding top-down parser, that is: sentence  noun-phrase, verb-phrase determiner  [the] will generate the following: sentence (I, O) :- noun-phrase(I,R), verb-phrase (R,O). determiner ([the|X], X) :- !.

24.Write a PROLOG program that will take grammar rules.

% PROLOG program that will take grammar rules.

%  below sets of  function is not predefined you can use any name as function

det([the|X],X):-!.
noun([sohan|X],X).
noun([frog|X],X).
verb([loves|X],X):-!.

sentence(P1,P3):- np(P1,P2),vp(P2,P3).
np(P1,P2):- noun(P1,P2).
np(P1,P3):- det(P1,P2),noun(P2,P3).
vp(P1,P3):- verb(P1,P2), np(P2,P3).

Write a program in PROLOG to implement merge (L1, L2, L3) where L1 is first ordered list and L2 is second ordered list and L3 represents the merged list.

23.Write a program in PROLOG to implement merge (L1, L2, L3) where L1 is first ordered list and L2 is second ordered list and L3 represents the merged list.

%PROLOG to implement merge (L1, L2, L3)

%  below merge function is not predefined you can use any name as function

merge([],[],[]).
merge([],L2,L2).
merge(L1,[],L1).
merge([H1|T1],[H2|T2],[H1|T3]):- H1=<H2,
merge(T1, [H2|T2], T3).
merge([H1|T1],[H2|T2],[H2|T3]):- merge([H1|T1], T2, T3).

% Output

Write a program in PROLOG to implement delete_all (X, L, R) where X denotes the element whose all occurrences has to be deleted from list L to obtain list R.

22.Write a program in PROLOG to implement delete_all (X, L, R) where X denotes the element whose all occurrences has to be deleted from list L to obtain list R.

%prolog program to implement delete_all(X,L,R)

%  below delete_all function is not predefined you can use any name as function

delete_all(X, [], []).
delete_all(X, [X|T], R):- delete_all(X, T, R).
delete_all(X, [H|T], [H|R]):- delete_all(X, T, R).

% Output


Write a Prolog program to implement delete_nth (N, L, R) that removes the element on Nth position from a list L to generate a list R.

21.Write a Prolog program to implement delete_nth (N, L, R) that removes the element on Nth position from a list L to generate a list R.

%Prolog program to implement delete_nth (N, L, R)

%  below delete_nth function is not predefined you can use any name as function

delete_nth(1, [H|T], T).
delete_nth(N, [H|T], [H|R]):- N1 is N-1,
delete_nth(N1, T, R).

% Output


Write a Program in PROLOG to implement sublist(S, L) that checks whether the list S is the sublist of list L or not. (Check for sequence or the part in the same order).

20.Write a Program in PROLOG to implement sublist(S, L) that checks whether the list S is the sublist of list L or not. (Check for sequence or the part in the same order).

%Program in PROLOG to implement sublist(S, L)

%  below subset function is not predefined you can use any name as function

subset([], L).
subset([E|Tail], [E|NTail]):-
subset(Tail, NTail).
subset([E|Tail], [D|NTail]):-
subset([E|Tail], NTail).


% Output


Write a prolog program to implement insert_nth(I, N, L, R) that inserts an item I into Nth position of list L to generate a list R.

19.Write a prolog program to implement insert_nth(I, N, L, R) that inserts an item I into Nth position of list L to generate a list R. 

%Write Prolog program to implement insert_nth(I, N, L, R) not.

%  below insert_nth function is not predefined you can use any name as function

insert_nth(I, 1, L, [I|L]).
insert_nth(I, N, [H|T], [H|R]):- N1 is N-1,insert_nth(I, N1, T, R).


% Output


Write a Prolog program to implement maxlist(L, M) so that M is the maximum number in the list.

18.Write a Prolog program to implement maxlist(L, M) so that M is the maximum number in the list. 
%Prolog program to implement maxlist(L, M)

%  below maxlist function is not predefined you can use any name as function

maxlist([H],H).
maxlist([H|T],R):-
 maxlist(T,M1),
 H>=M1,
 R is H.
maxlist([H|T],R):-
 maxlist(T,M1),
 H<M1, 
 R is M1.

% Output


Write a program in PROLOG to implement remove_dup (L, R) where L denotes the list with some duplicates and the list R denotes the list with duplicates removed.

17.Write a program in PROLOG to implement remove_dup (L, R) where L denotes the list with some duplicates and the list R denotes the list with duplicates removed. 

%PROLOG to implement remove_dup (L, R)

%  below is-member function is not predefined you can use any name as function

is_member(X,[X|_]).
is_member(X,[_|T]):- is_member(X,T).
remove_dups([],[]).
remove_dups([H|T], R):- is_member(H,T), remove_dups(T,R).
remove_dups([H|T],[H|R]):- \+(is_member(H,T)),remove_dups(T,R).


% Output



Write a Prolog program to implement nth_element (N, L, X) where N is the desired position, L is a list and X represents the Nth element of L.

16.Write a Prolog program to implement nth_element (N, L, X) where N is the desired position, L is a list and X represents the Nth element of L.

%Write Prolog program to implement nth_element (N, L, X) not.

%  below nth-element function is not predefined you can use any name as function

nth_element(1,[H|T],H).
nth_element(N,[H|T],X):- N1 is N-1,nth_element(N1,T,X).


% Output


Write a Prolog program to implement two predicates evenlength(List) and oddlength(List) so that they are true if their argument is a list of even or odd length respectively.

15.Write a Prolog program to implement two predicates evenlength(List) and oddlength(List) so that they are true if their argument is a list of even or odd length respectively.

%Write Write a Prolog program to implement two predicates evenlength(List) and 
oddlength(List) so that they are true if their argument is a list of even or 
odd length respectively.

%  below evenlength function is not predefined you can use any name as function

evenlength([]).
evenlength([_|T]):- oddlength(T).
oddlength([_]).
oddlength([_|T]):- evenlength(T).


% Output


Write a Prolog program to implement sumlist(L, S) so that S is the sum of a given list L.

14.Write a Prolog program to implement sumlist(L, S) so that S is the sum of a given list L.

% Write a Prolog program to implement sumlist(L, S) so that S is the sum of a given list L.

%  below sumlist function is not predefined you can use any name as function

sumlist([],0).
sumlist([H|T],S):- sumlist(T,S1),S is H+S1.

% Output


Write a Prolog program to implement reverse (L, R) where List L is original and List R is reversed list.

12.Write a Prolog program to implement reverse (L, R) where List L is original and List R is reversed list.

% Write a Prolog program to implement reverse (L, R) where List L is original 
  and List R is reversed list.

%  below reverse_list and reverse function is not predefined you can use any name as function

reverse_list(Inputlist,Outputlist):-
reverse(Inputlist,[],Outputlist).    
reverse([],Outputlist,Outputlist).    
reverse([Head|Tail],List1,List2):-
reverse(Tail,[Head|List1],List2).

% Output


Write a Prolog program to implement conc (L1, L2, L3) where L2 is the list to be appended with L1 to get the resulted list L3.

11.Write a Prolog program to implement conc (L1, L2, L3) where L2 is the list to be appended with L1 to get the resulted list L3.

% Write a Prolog program to implement conc (L1, L2, L3) where L2 is the 
  list to be appended with L1 to get the resulted list L3.

%  below conc function is not predefined you can use any name as function

conc([],L1,L1).
conc([X|T],L2,[X|T1]):- conc(T,L2,T1).


% Output


Write a Prolog program to implement memb(X, L): to check whether X is a member of L or not.

10. Write a Prolog program to implement memb(X, L): to check whether X is a member of L or not.

% Write a Prolog program to implement memb(X, L): to check whether
  X is a member of L or not.

%  below memb function is not predefined you can use any name as function

memb(X, [X|Tail]).
memb(X, [Head|Tail]):- memb(X, Tail).


% Output


Consider a cyclic directed graph [edge (p, q), edge (q, r), edge (q, r), edge (q, s), edge (s,t)] where edge (A,B) is a predicate indicating directed edge in a graph from a node A to a node B. Write a program to check whether there is a route from one node to another node.

9.Consider a cyclic directed graph [edge (p, q), edge (q, r), edge (q, r), edge (q, s), edge (s,t)] where edge (A,B) is a predicate indicating directed edge in a graph from a node A to a node B. Write a program to check whether there is a route from one node to another node.

% implement the cyclic directed graph.

%  below edge and path function is not predefined you can use any name as function

edge(p,q).
edge(q,r).
edge(r,s).
edge(s,t).
path(X,Y) :- edge(X,Y).
path(X,Y) :- edge(X,Z),
path(Z,Y).


% Output


Write a program in PROLOG to implement towerofhanoi (N) where N represents the number of discs.

8. Write a program in PROLOG to implement towerofhanoi (N) where N represents the number of discs.

% Write a program in PROLOG to implement towerofhanoi (N) where N 
  represents the number of discs.

%  below hanoi function is not predefined you can use any name as function

hanoi(N,F,T,A):-
( N=1 ->
( write('Move disk 1 from rod '),write(F),write(' to rod '),write(T),nl
)
;
Z is N-1,
hanoi(Z,F,A,T),
write('Move disk 1 from rod '),write(F),write(' to rod '),write(T),nl,
hanoi(Z,A,T,F)
).

% Output


Prolog program to implement multi (N1, N2, R) : where N1 and N2 denotes the numbers to be multiplied and R represents the result.

7. Write Prolog program to implement multi (N1, N2, R) : where N1 and N2 denotes the numbers to be multiplied and R represents the result.

% Write Prolog program to implement multi (N1, N2, R) : where N1 and N2 denotes the 
  numbers to be multiplied and R represents the result.

%  below m function is not predefined you can use any name as function

m(N,1,N).
m(N,M,A):-
T is M-1,
m(N,T,Y),
A is Y+N.

% Output



Write a Prolog program to implement power (Num,Pow, Ans) : where Num is raised to the power Pow to get Ans.

6. Write a Prolog program to implement power (Num,Pow, Ans) : where Num is raised to the power Pow to get Ans.

% Write a Prolog program to implement power (Num,Pow, Ans) : where 
  Num is raised to the power Pow to get Ans.

%  below power function is not predefined you can use any name as function

power(0,P,0):- P>0.
power(X,0,1):- X>0.
power(X,P,A):- X>0,P>0,P1 is P-1,
power(X,P1,Ans),
A is Ans*X.

% Output


Write a Prolog program to implement GCD of two numbers.

5. Write a Prolog program to implement GCD of two numbers.

%Write a Prolog program to implement GCD of two numbers.

%  below gcd function is not predefined you can use any name as function

gcd(X,Y):-
( X=0->
write(Y)
 ;
 Y=0->
write(X)
 ;
X=Y->
write(X)
;
X>Y ->
  ( Z is X-Y,
 gcd(Z,Y)
  )
  ;
Y>X ->
  (C is Y-X,
  gcd(X,C)
  )
).

% Output


Write a program in PROLOG to implement generate_fib(N,T) where T represents the Nth term of the fibonacci series.

4. Write a program in PROLOG to implement generate_fib(N,T) where T represents the Nth term of the fibonacci series.

% Write a program in PROLOG to implement generate_fib(N,T) where T 
  represents the Nth term of the fibonacci series.

%  below generate_fib function is not predefined you can use any name as function

generate_fib(0,1).
generate_fib(1,1).
generate_fib(N,T):- N1 is N-1, 
generate_fib(N1,T1),
N2 is N-2, 
generate_fib(N2,T2), 
T is T1+T2.

% Output


Featured post

Amazon Interview Process

On July 5, 1994, Jeff Bezos started the world's most "customer-centric" firm out of his garage in Bellevue, Washington. The A...