CSC 17 07 Suman
CSC 17 07 Suman
toh(N):-mov(N,left,center,right).
%route(X,Y):-edge(X,Y),write(X),write("->"),write(Y),!.
route(X,Y):-edge(X,Z),write(X),write("->"),route(Z,Y).
Output:
Output:
Output:
Commands to go back from trace.
revlist([],[]).
revlist([H|T],R):-revlist(T,Trev),conc(Trev,[H],R).
Output:
Q13. Check whether a list is palindrome or not.
Source code:
conc([],L,L).
conc([H|T],L2,[H|L3]):-conc(T,L2,L3).
revlist([],[]).
revlist([H|T],R):-revlist(T,Trev),conc(Trev,[H],R).
Output:
Output:
Q15. Even length or odd length list.
Source Code:
len([],0).
len([H|T],N) :- len(T,N1), N is N1+1.
evenlength(List):- len(List,N),mod(N,2)=:=0,write("The list ids even length").
oddlength(List):-len(List,N),mod(N,2)=\=0,write("The list is odd length").
check_list(L):- evenlength(L);oddlength(L).
/* ; mean either goal can be true */
Output:
nth_element(1,[H|T],H).
nth_element(N,[H|T],X):-N1 is N-1, nth_element(N1,T,X).
Output:
Q17. Write a Prolog program to implement remdup( L, R) to remove duplicates from a
list L to generate a list R.
Source code:
ismember(X,[X|T]).
ismember(X,[H|T]):-ismember(X,T).
remove_dups([],[]).
remove_dups([H|T],R):-ismember(H,T),remove_dups(T,R).
remove_dups([H|T],[H|R]):-not(ismember(H,T)),remove_dups(T,R).
Output:
maxlist([H|[]], H).
maxlist([H|T],M) :- maxlist(T,M1), max(H,M1,M).
Output:
Q19. 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.
Source code :
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 :
Q20. 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).
Source code:
sublist([],L).
sublist(S,[]):- false.
sublist([H1|T1],[H1|T2]):- sublist(T1,T2).
sublist([H1|T1],[H2|T2]):- sublist([H1|T1],T2).
Output:
Q21. : 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.
Source code:
delete_nth(1, [H|T], T).
delete_nth(N, [H|T], [H|R]):- N1 is N-1,delete_nth(N1, T, R).
Output:
Output:
Q23. 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.
Source code:
merge([],L,L):-!,write("Abc").
merge([H1|T1],[H2|T2],[H3|T3]):-H1>H2,H3 is H2,merge([H1|T1],T2,T3).
merge([H1|T1],[H2|T2],[H3|T3]):-H1<H2,H3 is H1,merge(T1,[H2|T2],T3).
merge([H1|T1],[H2|T2],[H3,H4|T3]):-H1=H2,H3 is H1,H4 is H2,merge(T1,T2,T3).
Output:
Q24. 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) :- !.
Source code:
sentence-->np,vp.
np-->det,noun.
vp-->verb.
vp-->verb,np.
det-->[a].
det-->[the].
det-->[an].
noun-->[boy].
noun-->[girl].
noun-->[song].
noun-->[apple].
verb-->[sing].
verb-->[sings].
verb-->[eats].
Output:
%Rules%
animal(X):- mammal(X).
animal(X):- bird(X).
mammal(X):- cat(X).
have(X,fur):- mammal(X).
likes(X,cream):- cat(X).
Output:
Source code:
(b)
ako(mammal,animal).
ako(bird,mammal).
ako(elephant,mammal).
is_a(tweety,bird).
is_a(clyde,elephant).
is_a(neil,elephant).
can(animal,breathe).
can(animal,move).
can(bird,fly).
can(tweety,sing).
has(neil,three_legs).
has(bird,feather).
has(bird,wing).
has(elephant,four_legs).
has(elephant,one_trunk).
has(elephant,one_tail).
has(animal,skin).
has(mammal,head).
color(elephant,grey).
color(tweety,yellow).
like(neil,apple).
%Rules
check(O,P,V):- G=..[P,O,V],G,!;!.
check(O,P,V):-is_a(O,X),check(X,P,V);ako(O,X),check(X,P,V).
Output: