List in Prolog


It is important for any programming tool to have some functionality of handling collections like array, list or something else. Prolog use lists for the very purpose and I must warn C/C++ programmers (or even Java, C# or any other C-like language fan) that lists are not arrays but similar to it.

A list must be declared as list type in domains section or in predicate section, it should be mentioned that we are using list.

In domains section, we do it by declaring a new domain and equaling it to a list type, declared by name of type followed by an asterisk; e.g.:

numList=integer*

if one like to avoid domain section, but use list it should be mentioned in predicate section like this:

somePredicate(integer*)

A list is presented as a comma-separated bracket closed tuple of same type:

[a,b,c,]

[1,15,2,100,11,12,19]

[“Car”,“House”,”pen”,”tree”,”Etc”]

List is not random access container but can be accessed in sequence. First element of a list can be accessed in kind of weird way:

[Head|Tail]

If we pass a list there, Head will contain the first element while Tail contains another list excluding the first elemetn. Hence, we need to write some extra lines of code to access nth element.

I would like to avoid further explanation, which you can find a lot in the internet, but will go forward to placing an example code:

/*nafSadh.org*/
/*Example code on LIST in Prolog*/
domains

numList=integer*
key,index,element,minimum,maximum,sum,count=integer
average=real

predicates

/*Traverse list*/
trav(numList)
/*Search an element in the list*/
search(numList,key,index)
search(numList,key)
/*Get the last element of list*/
last(numList,element)
last(numList)
/*Get nth element of list*/
nth(numList,index)
/*get the minimum valued element out of list*/
min(numList)
min(numList,minimum)
/*get the maximum valued element out of list*/
max(numList,maximum)
max(numList)
/*find the average value of elements in list*/
avg(numList)
avg(numList,average)
avg(numList,sum,count)

clauses

/*TRAV*/
trav([]):-
nl.
trav([H|Tail]):-
write(H,”,”),trav(Tail).

/*SEARCH*/
search(List,Key):-
search(List,Key,1).
search([],_,_):-
write(“Not Found”),nl.

search([H|_],Key,I):-
H=Key,write(“Found at “,I),nl.

search([_|T],Key,I):-
II=I+1,search(T,Key,II).

/*LAST*/
last(List):-
last(List,E),write(E),nl.

last([Head],E):-
E=Head.
last([_|Tail],E):-
last(Tail,E).

/*NTH ELEM*/
nth([],_):-
write(“overflow”),nl.

nth([Head|_],I):-
I=1,write(Head),nl.

nth([_|Tlist],I):-
II=I-1,nth(Tlist,II).

/*MIN*/
min([H|T]):-
min(T,H).
min([],Min):-
write(Min),nl.
min([H|T],Min):-
H<Min,M1n=H,min(T,M1n).
min([_|T],Min):-
min(T,Min).

/*MAX*/
max([H|T]):-
max(T,H).
max([],Max):-
write(Max),nl.
max([H|T],Max):-
H>Max,M4x=H,max(T,M4x).
max([_|T],Max):-
max(T,Max).

/*AVG*/
avg([]):-
write(“Empty List”),nl.
avg(List):-
avg(List,Avg),write(Avg),nl.
avg(List,Avg):-
avg(List,Sum,C),Avg=Sum/C.
avg([H],Sum,C):-
Sum=H,C=1.
avg([H|T],Sum,C):-
avg(T,S,Count),Sum=S+H,C=Count+1.

Happy Prolog!

Unlike some of my fellow bloggers, I hold pure authorship to the codes placed here and these codes have never been cut pasted from some other site or plagiarized from any fellow student. Being frightened by some web publisher, I hereby have to claim my copyright on elements presented here.

All elements published here are published under NS7 Open Content License, which express that,

  • if anyone use the content, partial or full in any context, s/he shall must mention the source and refer to the hyperlink
  • any code under this license must not be published in any media unless modified significantly and that modification executes well enough without harm and credit to actual author is mentioned
  • if anyone need to use code, published under this license,  as example s/he can do it by mentioning actual author and shall not host a source file but refer to the original one, with another hyperlink to content accompanying, if accompanies one, the very code. As this code shall be used as example, content text should be relevant and example code should not be more than 40% of total content.
  • Mentions to authors of codes and contents under this license must be in a good manner. Mention should be in same font of normal text of the content used in, font size should be of same size or larger than normal text of the content but in any case minimum size in electronic media is 12px or 11pt in print media. Normal text is regarded as that kind of text that comprises of a minimum of 70% of total content.
  • contents presented here can be used for personal and academic purpose without any mention but shall not be published without taking proper permission from actual author
Advertisements

3 thoughts on “List in Prolog

  1. What do I write in domain if I need to insert a list as list of list of integers, e.g., I want to have a variable which can take the value [1,2,[3,4],[], [[[5]]], 6]. Compiler Turbo Prolog. Thank You.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s