Lists
Lists are powerful data structures for holding and manipulating groups of things. In Prolog, a list is simply a collection of terms. The terms can be any Prolog data types,including structures and other lists.Syntactically, a list is denoted by square brackets with the terms separated by commas. For example, a list of alcohol is represented as
[Tequila,Whisky,Vodka,Martini,Muscat,Malibu,Soho,Epita]
This gives us an alternative way of representing the locations of things. Rather than having separate location predicates for each thing, we can have one location predicate per container, with a list of things in the container.
list_where([Tequila,Whisky,Vodka], bathroom).
list_where([Martini,Muscat], kitchen).
list_where([Malibu,Soho], under_the_bed).
list_where([Epita], everywhere).
The empty list is represented by a set of empty brackets []. This is equivalent to the nil in other programming language.For our example in this section, it can describe the lack of things in a place :
list_where([], cave).
The Unification works on lists just as it works on other data structure of SWI Prolog. With that, we now can ask questions about lists to prolog:
?- [_,_,X] = [lesson, work, sleeping].
X = sleeping
?- list_where(X, under_the_bed).
X = [Malibu,Soho]
Notice that there is a impractical method of getteing a list of elements in the first example, because of Prolog won't unify unless both list have the same number of elements. At last, the special notation for list structures.
[X | Y]
This structure is unified with a list, X is bound to the first element of the list, called the head. Y is bound to the list of remaining elements, called the tail.Note that the tail is considered as a list for Prolog and the empty list does not unify with the standart list syntax because it has no head. Here is an example :
?- [X|Y] = [a, b, c, d, e].
X = a
Y = [b, c, d, e]
?- [X|Y] = [].
no
The empty list does not unify with the standard list syntax because it has no head.
?- [X|Y] = [].
no
This failure is important, because it is often used to test for the boundary condition in a recursive routine. That is, as long as there are elements in the list, a unification with the [X|Y] pattern will succeed. When there are no elements in the list, that unification fails, indicating that the boundary condition applies. We can specify more than just the first element before the bar (|). In fact, the only rule is that what follows it should be a list.
?- [First, Second | Q] = [water,gin,tequila,whisky].
First = water
Second = gin
Q = [tequila,whisky]
We have said a list is a special kind of structure. In a sense it is, but in another sense it is just like any other Prolog term. The last example gives us some insight into the true nature of the list. It is really an ordinary two-argument predicate. The first argument is the head and the second is the tail. This predicate is represented by a period(.). To see this notation, we use the built-in predicate display, which writes lits in using this syntax.
?- X = [T|Q], display(X).
.(_01,_02)
From this examples it should be clear why there is a different syntax for lists. The easier syntax makes for easier reading, but sometimes obscures the behavior of the predicate. It helps to keep this "real" structure of lists in mind when working with predicates that manipulate lists.
1. A program to add element in a list.
Code:
add(L1,R,[R|L1]).
add([X|L1],R,[X|L2]):-add(L1,R,L2).
2. A program to delete element of a list.
Code:
delet(Y,[Y|L],L).
delet(Y,[H|L1],[H|L2]):-delet(Y,L1,L2).
3. A program to create member of the list.
Code:
mem(X,[X|L]).
mem(X,[H|T]):-mem(X,T).
4. A program to count no. of elements in a list.
Code:
count([],0).
count([H|L1],N):-count(L1,M), N is M+1.
5. A program to find sum of elements of a list.
Code:
sum([],0).
sum([H|T],S):-sum(T,S1), S is S1+H.
6. A program to find reverse of a list.
Code:
reverse([],L,L).
reverse([H|L1],L2,L3):-reverse(L1,[H|L2],L3).
No comments:
Post a Comment