Artificial Intelligence tutorial 7
a. Write a program to split the list on the basis of a given integer into
greater and smaller number.
domains
numlist = integer*
predicates
go(numlist,numlist,numlist)
list_split(integer,numlist,numlist,numlist)
clauses
go(InpL,SL,GL):-
write("Enter pivot number :
"),
readint(Num),
list_split(Num,InpL,SL,GL).
list_split(Num,[],[],[]).
list_split(Num,[Head|Tail1],[Head|Tail2],GL):-
Num <= Head,
list_split(Num,Tail1,Tail2,GL).
list_split(Num,[Head|Tail1],SL,[Head|Tail2]):-
Num > Head,
list_split(Num,Tail1,SL,Tail2).
b.
Write a program to
substitute an element in the list by a given another element.
domains
numlist = integer*
predicates
go(numlist,numlist)
list_sub(integer,integer,numlist,numlist)
clauses
go(InL,OutL):-
write("Enter element to use :
"),
readint(Num),
write("Enter element to replace :
"),
readint(NumRep),
list_sub(Num,NumRep,InL,OutL).
list_sub(Num,NumRep,[],[]).
list_sub(Num,NumRep,[Head1|Tail1],[Num|Tail2]):-
NumRep = Head1,
list_sub(Num,NumRep,Tail1,Tail2).
list_sub(Num,NumRep,[Head1|Tail1],[Head1|Tail2]):-
NumRep <> Head1,
list_sub(Num,NumRep,Tail1,Tail2).
c.
Write a program to separate
the odd-even number form the given list.
domains
numlist =
integer*
predicates
list_oddeven(numlist,numlist,numlist)
clauses
list_oddeven([],Odd,Even).
list_oddeven([H|T],[H|Odd],Even):-
N = H mod 2,
N <> 0,
list_oddeven(T,Odd,Even).
list_oddeven([H|T],Odd,[H|Even]):-
N = H mod 2,
N = 0,
list_oddeven(T,Odd,Even).