Thursday, June 21, 2012

Visual Prolog Program to delete a given element from the list.

To delete a element from a list the position and the element list is passed to the predicate delete. Then the delete predicate continuously breaks the list into head (H) and Tail (T) to find that the correct position is reached. On reaching the appropriate position to delete the element the predicate slices the head off the list and finally merges the remaining art with the rest. The prolog code is given as below:

int_list = integer*

del(integer, int_list, int_list)

del(0,[H|T], T).
del(P,[H|T], [H|Z]):-
P1 = P - 1, del(P1,T,Z).

del(2, [0,1,2,3,4,5],X).

No comments:

Post a Comment