Sunday, 16 June 2019

Introduce concept of backtracking and negation/ failure also explain CUT. GTU PROLOG ARTIFICIAL INTELLEGENCE



1.  Write a program to insert an element to list without duplication using CUT.

add(Element, List, List) :-member(Element, List), !. add(Element, List, [Element | List]).

Input: add(dog, [dog, donkey, rabbit], List).
Output: List = [dog, donkey, rabbit].

Input: add(cat, [dog, donkey, rabbit], List).
Output: List = [cat, dog, donkey, rabbit].








2.  write a list into two lists: a list with positive numbers[including zero] and a list with negative numbers. [use CUT where necessary.]



split([],[],[]).
split([X|L],[X|L1],L2):- X>= 0,
!,
split(L,L1,L2).

split([X|L],L1,[X|L2]):-
split(L,L1,L2).

Input: split([1,2,-3,4,-5,2],X,Y).
Output: X = [1, 2, 4, 2],
Y = [-3, -5].

No comments:

Post a Comment

It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...