List:
Lists is an important recursive data structure which is widely used in computational linguistics.
It is a finite sequence of elements. We can specify lists in Prolog by enclosing the elements of the list in square brackets(that is, the symbols [ and ]). The elements are separated by commas.
All sorts of Prolog objects can be elements of a list and the same item may occur more than once in the same list.
The empty list(as its name suggests) is the list that contains no elements.
lists can contain other lists as elements.
Any non-empty list can be thought of as consisting of two
parts: the head and the tail.
The head is simply the first item in the list; the tail is everything else.
1. Write a program to add to a list.
Add list :add(L1,R,[R|L1]).
add([X|L1],R,[X|L2]):-add(L1,R,L2).
--
=======================================================
2. Write a program to count the number of item in the list.
Count:
count([],0).
count([H|T],N):-count(T,B),N is B+1.
count([H|T],N):-count(T,B),N is B+1.
=========================================================
3. Write a program to delete from a list.
Delete:
del(X,[X|L1],L1).
del(X,[H|L1],[H|L2]):-del(X,L1,L2).
del(X,[H|L1],[H|L2]):-del(X,L1,L2).
===========================================================
4. Write a program to check the member of the list.
Member:
memb(X,[X|L]).
memb(X,[H|T]):-memb(X,T).
memb(X,[H|T]):-memb(X,T).
============================================================
5. Write a program to reverse a list.
Reverse:
reverse(L1,L2):-reverse(L1,L2,[]).
reverse([],L1,L2).
reverse([],L1,L2).
============================================================
6. Write a program to summing a list.
Sum:
sum([],0).
sum([H|T],N):-sum(T,B),N is B+H.
sum([H|T],N):-sum(T,B),N is B+H.
============================================================
No comments:
Post a Comment