next up previous
Next: Lists - Use Up: No Title Previous: Back to Lists

Using Conc for more list functions

We can write a procedure for deleting elements from within a list

delete( X, List, Result) :-
 conc( L1, [X| L2], List),
 conc( L1, L2, Result).

Sample query :

 delete(2, [1,2,3,4], Result) 
 Result = [1,3,4]

Similarly, one can insert elements into the list :

 insert(X, List, Result) :-
   delete(X, Result, List).

How does this work ? Try developing the definition in terms of the conc definition earlier

It is also possible to look for an arbitrary sublist within the list, this can again be achieved by using the conc definition earlier. Hence :

sublist(List, Sublist) :-
 conc(_, L1, List),
 conc( Sublist, _, L1).

A query of the form :

sublist([1,2,3,4,5,6],[3,4,5])
yes



Omer F Rana
Fri Feb 14 20:23:31 GMT 1997